ale*_*ale 4 c# timespan console-application
C#我需要显示进程正在运行的时间,显示秒数增加,通常:00:00:01、00:00:02、00:00:03.....等。
我正在使用此代码:
var stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
//here is doing my process
stopwatch.Stop();
Run Code Online (Sandbox Code Playgroud)
当进程停止时,我显示时间 ELAPSED,如下:
TimeSpan ts = stopwatch.Elapsed;
Run Code Online (Sandbox Code Playgroud)
...和这个:
{0} minute(s)"+ " {1} second(s)", ts.Minutes, ts.Seconds, ts.Milliseconds/10.
Run Code Online (Sandbox Code Playgroud)
这显示经过的总时间,但我需要显示在控制台中运行的时间。
我怎样才能做到这一点?
尝试
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
while (true)
{
Console.Write(stopwatch.Elapsed.ToString());
Console.Write('\r');
}
Run Code Online (Sandbox Code Playgroud)
要防止显示毫秒:
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
while (true)
{
TimeSpan timeSpan = TimeSpan.FromSeconds(Convert.ToInt32(stopwatch.Elapsed.TotalSeconds));
Console.Write(timeSpan.ToString("c"));
Console.Write('\r');
}
Run Code Online (Sandbox Code Playgroud)