我需要显示在控制台中运行的计时器。我用秒表:
System.Diagnostics.Stopwatch秒表=新的System.Diagnostics.Stopwatch();
public void startTime()
{
stopwatch.Start();
}
public void GetTime(){
stopwatch.Stop();
TimeSpan timeSpan = stopwatch.Elapsed;
var timeElapsed = (string.Format("\nTime Elapsed: {0} minute(s) {1} second(s)",
timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds / 10 + "\n")).ToString();
}
我需要在控制台中显示正常运行的计时器:00:00:01,00:00:02,00:00:03 ......等等
我该怎么做?
答案 0 :(得分:2)
见
Console.Clear
和
Console.SetCursorPosition
方法
答案 1 :(得分:0)
这样的事情?
Stopwatch sw = new Stopwatch();
sw.Start();
while (true)
{
Console.SetCursorPosition(0, 0);
Console.Write(sw.Elapsed.ToString("g"));
if (sw.Elapsed.TotalMinutes > 30)
break;
}