Question

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 ?

Était-ce utile?

La 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);
}

Autres conseils

You should be able to achieve what you want with something like

Console.Write("Time :  "+chrono.Elapsed+"           \r");

You can store the Console.CursorLeft and Console.CursorTop properties to local integer variables. Then before calling Console.WriteLine() set the cursor position in the console by using Console.SetCursorPosition().

Example:

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

        bool isMyComputerOn = true;
        int cursorCol = Console.CursorLeft;
        int cursorRow = Console.CursorTop;
        while (isMyComputerOn)
        {
            Console.SetCursorPosition(cursorCol, cursorRow);
            Console.WriteLine("Time :  " + chrono.Elapsed);
        }

Here is the answer to an similar question:

for(int i = 60; i >= 0; i--)
{
Console.Write("\r{0}%   ", i);
}

How can I update the current line in a C# Windows Console App?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top