-1

我正在考虑这样做:

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

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

但我不知道如何计算出使秒表以秒为单位的常数。

4

3 回答 3

3

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

var watch = StopWatch.StartNew();

while(true)
{
    Console.WriteLine(watch.ElapsedMilliseconds / 1000f + " seconds");
}
于 2013-07-12T19:08:34.953 回答
1

您可以在System.Diagnostics 命名空间中使用 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);
于 2013-07-12T19:11:22.543 回答
0

猜测你想要什么,做:

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

while(true)
{
    Console.WriteLine(sw.ElapsedMilliseconds / 1000 + " seconds");
}
于 2013-07-12T19:08:43.220 回答