私はやろうと思っていました:
int intCount = 0;
int intConstant = ...;
while(true)
{
Console.WriteLine(intCount / intConstant + " seconds");
}
しかし、ストップウォッチを秒単位でカウントするための定数を計算する方法がわかりません。
私はやろうと思っていました:
int intCount = 0;
int intConstant = ...;
while(true)
{
Console.WriteLine(intCount / intConstant + " seconds");
}
しかし、ストップウォッチを秒単位でカウントするための定数を計算する方法がわかりません。
ループは使用しないでください。これはプロセッサ固有のものです。StopWatch
クラスをより適切に使用します。
var watch = StopWatch.StartNew();
while(true)
{
Console.WriteLine(watch.ElapsedMilliseconds / 1000f + " seconds");
}
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);
あなたが望むものを推測して、次のようにします:
Stopwatch sw = new Stopwatch();
sw.Start();
while(true)
{
Console.WriteLine(sw.ElapsedMilliseconds / 1000 + " seconds");
}