在控制台中显示计时器(分钟和秒)

时间:2011-05-17 19:27:07

标签: c# timer stopwatch

我需要显示在控制台中运行的计时器。我用秒表:

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 ......等等

我该怎么做?

2 个答案:

答案 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;
}