如何制作秒表控制台?

时间:2013-07-12 19:05:46

标签: c# console-application

我在考虑做:

int intCount = 0;
int intConstant = ...;

while(true)
{
    Console.WriteLine(intCount / intConstant + " seconds");
}

但我不知道如何计算出秒表的秒数。

3 个答案:

答案 0 :(得分:3)

不要使用循环,这将是特定于处理器的。更好地使用StopWatch类:

var watch = StopWatch.StartNew();

while(true)
{
    Console.WriteLine(watch.ElapsedMilliseconds / 1000f + " seconds");
}

答案 1 :(得分:1)

您可以在System.Diagnostics namespace

中使用StopWatch类
        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();
        Thread.Sleep(10000);
        stopWatch.Stop();
        // Get the elapsed time as a TimeSpan value.
        TimeSpan ts = stopWatch.Elapsed;

        // Format and display the TimeSpan value. 
        string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
            ts.Hours, ts.Minutes, ts.Seconds,
            ts.Milliseconds / 10);
        Console.WriteLine("RunTime " + elapsedTime);

答案 2 :(得分:0)

猜测你想要的东西,做:

Stopwatch sw = new Stopwatch();
sw.Start();

while(true)
{
    Console.WriteLine(sw.ElapsedMilliseconds / 1000 + " seconds");
}