1

i am writing a simple program, and for that i want to output the current cpu usage, however with a stander "for" or "while" loop, it prints it every time on a new line, let me show what i mean:

output should be:

Current cpu usage: (usage)

where the usage refreshes every second

now, i want to refresh it every second, but as i mentioned, with a for or while loop that wil just print it out to a new line every time, like this:

Current cpu usage: (usage)
(usage)
(usage)
(usage)
(usage)

so, how do i just refresh the "usage"?

btw, i'm pretty familiar with c#, so you don't have to go in depth :)

Thanks

Omer Enes
  • 93
  • 1
  • 6

2 Answers2

8

Use the \r character alone to return the caret ("print head") to the first column. This is instead of writing \r\n (a Windows/DOS new-line sequence) or \n (Unix/Linux) which give you a new-line.

Internally, Console.WriteLine( String x ) is the same thing as Console.Write( String x ); Console.Write( Environment.NewLine ); (and Environment.NewLine is "\r\n" on Windows).

Try this:

while( true )
{
    Single cpuUsage = GetCpuUsage();
    Console.Write( "Current CPU usage: {0,5:P2}", cpuUsage );
    Console.Write( "\r" );

    await Task.Delay( 500 );
}

Note that you might need to print out a line of whitespace to overwrite any previous text if the next line will be shorter than the previous line, because the previous text will be there. (But by using {0,5:P2} it guarantees that that text will always take up 5 characters (regardless of if it's 1% or 100% - or use {0,-5:P2} for a left-aligned number instead).

This technique also works in other platforms that use the same stdin/stdout semantics as C#/.NET, including C, C++, Java, and so on - whereas the Console.SetCursorPosition API is not as universal.

Dai
  • 141,631
  • 28
  • 261
  • 374
  • Neat, I wasn't aware that the C# console responded to `\r`. Is this essentially the same as using `SetCursorPosition` to 0 (as in Rufus' answer)? __Edit:__ Your edit answers my question. – RToyo May 01 '19 at 18:21
  • 1
    @RToyo Essentially it works the same, but `SetCursorPosition` is more expensive (see source code [here](https://referencesource.microsoft.com/mscorlib/R/114298628fe9afcf.html)). – Rufus L May 01 '19 at 18:25
  • 1
    @RToyo There might be a difference in terms of redirecting output (of a Console app); you can't seem to redirect the output to a file if you use 'SetCursorPosition()' (- should you need to redirect/capture the output). – DennisVM-D2i May 23 '22 at 08:12
7

You aren't showing the code where you're writing the output to the console, but if you don't want to write a new line, you can use a combination of Console.Write(message); and Console.SetCursorPosition (to reset the cursor to the start of the line again).

For example:

for (int i = 0; i < 100000; i ++)
{
    // Set the cursor to the beginning (0) of the current line (Console.CursorTop)
    Console.SetCursorPosition(0, Console.CursorTop);
    Console.Write("count: " + i);
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43