开发者

Shows timer (minutes and seconds) in console

开发者 https://www.devze.com 2023-03-06 19:25 出处:网络
I need shows the timer running in console. I use开发者_StackOverflow中文版 stopwatch: System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();

I need shows the timer running in console. I use开发者_StackOverflow中文版 stopwatch:

System.Diagnostics.Stopwatch stopwatch = new 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();
}

I need shows in console the timer running normally: 00:00:01, 00:00:02, 00:00:03 ... etc

How can I do this?


see

Console.Clear

and

Console.SetCursorPosition

methods


something like this?

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

while (true)
{
    Console.SetCursorPosition(0, 0);
    Console.Write(sw.Elapsed.ToString("g"));

    if (sw.Elapsed.TotalMinutes > 30)
        break;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号