Search code examples
c#.netconsolestopwatch

How to make a Stopwatch without rewrite line for each display


I want to display a stopwatch but i don't want to clean my console for each dislay ,

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

            bool isMyComputerOn = true;
            while (isMyComputerOn)
            {
                Console.WriteLine("Time :  "+chrono.Elapsed);
            }

Any idea ?


Solution

  • You must use SetCursorPosition to fix the position of you cursor

    Stopwatch chrono = new Stopwatch();
    chrono.Start();
    bool isMyComputerOn = true;
    while (isMyComputerOn)
    {
       Console.SetCursorPosition(1,1);
       Console.WriteLine("Time : "+chrono.Elapsed);
    }