When building a Windows Console App in C#, is it possible to write to the console without having to extend a current line or go to a new line? For example, if I want to show a percentage representing how close a process is to completion, I'd just like to update the value on the same line as the cursor, and not have to put each percentage on a new line.
Can this be done with a "standard" C# console app?
You can use
Console.SetCursorPosition
to set the position of the cursor and then write at the current position.Here is an example showing a simple "spinner":
Note that you will have to make sure to overwrite any existing output with new output or blanks.
Update: As it has been criticized that the example moves the cursor only back by one character, I will add this for clarification: Using
SetCursorPosition
you may set the cursor to any position in the console window.will set the cursor to the beginning of the current line (or you can use
Console.CursorLeft = 0
directly).If you want update one line, but the information is too long to show on one line, it may need some new lines. I've encountered this problem, and below is one way to solve this.
i was looking for same solution in vb.net and i found this one and it's great.
however as @JohnOdom suggested a better way to handle the blanks space if previous one is larger than current one..
i make a function in vb.net and thought someone could get helped ..
here is my code:
Explicitly using a Carrage Return (\r) at the beginning of the line rather than (implicitly or explicitly) using a New Line (\n) at the end should get what you want. For example:
I just had to play with the divo's
ConsoleSpinner
class. Mine is nowhere near as concise, but it just didn't sit well with me that users of that class have to write their ownwhile(true)
loop. I'm shooting for an experience more like this:And I realized it with the code below. Since I don't want my
Start()
method to block, I don't want the user to have to worry about writing awhile(spinFlag)
-like loop, and I want to allow multiple spinners at the same time I had to spawn a separate thread to handle the spinning. And that means the code has to be a lot more complicated.Also, I haven't done that much multi-threading so it's possible (likely even) that I've left a subtle bug or three in there. But it seems to work pretty well so far:
So far we have three competing alternatives for how to do this:
I've always used
Console.CursorLeft = 0
, a variation on the third option, so I decided to do some tests. Here's the code I used:On my machine, I get the following results:
Additionally,
SetCursorPosition
caused noticeable flicker that I didn't observe with either of the alternatives. So, the moral is to use backspaces or carriage returns when possible, and thanks for teaching me a faster way to do this, SO!Update: In the comments, Joel suggests that SetCursorPosition is constant with respect to the distance moved while the other methods are linear. Further testing confirms that this is the case, however constant time and slow is still slow. In my tests, writing a long string of backspaces to the console is faster than SetCursorPosition until somewhere around 60 characters. So backspace is faster for replacing portions of the line shorter than 60 characters (or so), and it doesn't flicker, so I'm going to stand by my initial endorsement of \b over \r and
SetCursorPosition
.