I've seen this method that can clear a line without flicker, but is there a way to do the same to the whole console? (instead of using Console.Clear)
3 Answers
You can use Console.WindowLeft
/ Console.WindowTop
/ Console.WindowHeight
/ Console.WindowWidth
msdn to get the size of the currently visible region of the screen buffer.
Then you just need to replace each of those characters with a space like the answer you linked demonstrated.
e.g.:
public static void ClearVisibleRegion()
{
int cursorTop = Console.CursorTop;
int cursorLeft = Console.CursorLeft;
for(int y = Console.WindowTop; y < Console.WindowTop + Console.WindowHeight; y++) {
Console.SetCursorPosition(Console.WindowLeft, y);
Console.Write(new string(' ', Console.WindowWidth);
}
Console.SetCursorPosition(cursorLeft, cursorTop);
}
this will clear everything currently visible on the screen and return the cursor back to its original position.
If you want the cursor to move to the top left corner instead, you can do the following:
Console.SetCursorPosition(Console.WindowLeft, Console.WindowTop);
this might still result in flickering though, since it'll take some time to clear all the lines.
If you want to avoid flickering completely the only way is to draw whatever you want to display off-screen and then copy over the entire screen at once. That'll completely get rid of any flickering.
You can accomplish this by calling SetCursorPosition()
with a position that's outside your WindowLeft
/ WindowTop
/ WindowHeight
/ WindowWidth
rect (but still within BufferHeight
/ BufferWidth
).
Then draw an entire window worth of content.
Then call Console.MoveBufferArea()
to copy the content over into the current window area.

- 9,420
- 2
- 23
- 40
If you can use the latest Visual Studio along with C# 9. I would recommend using some source-generators with the CsWin32 package if this is a windows only project.
I use this to blit to the console. Extremely performant.
using Windows.Win32;
using Windows.Win32.System.Console;
public static void ClearConsole()
{
var rows = (short)Console.WindowHeight;
var cols = (short)Console.WindowWidth;
SafeFileHandle h = PInvoke.CreateFile("CONOUT$",
Windows.Win32.Storage.FileSystem.FILE_ACCESS_FLAGS.FILE_GENERIC_READ | Windows.Win32.Storage.FileSystem.FILE_ACCESS_FLAGS.FILE_GENERIC_WRITE,
Windows.Win32.Storage.FileSystem.FILE_SHARE_MODE.FILE_SHARE_WRITE,
null,
Windows.Win32.Storage.FileSystem.FILE_CREATION_DISPOSITION.OPEN_EXISTING,
Windows.Win32.Storage.FileSystem.FILE_FLAGS_AND_ATTRIBUTES.FILE_ATTRIBUTE_NORMAL,
null
);
if (!h.IsInvalid)
{
var screenBuffer = new CHAR_INFO[rows * cols];
var writeRegion = new SMALL_RECT
{
Left = 0,
Top = 0,
Right = (short)(cols),
Bottom = (short)(rows)
};
for (int y = 0; y < rows; y++)
{
for (int x = 0; x < cols; x++)
{
screenBuffer[y * cols + x].Attributes = (byte)Console.BackgroundColor;
//buf[y * cols + x].Char.UnicodeChar = '';
}
}
PInvoke.WriteConsoleOutput(
h,
screenBuffer[0],
new COORD { X = cols, Y = rows },
new COORD(),
ref writeRegion
);
}
}
My NativeMethods.txt
file:
CreateFile
WriteConsoleOutput
Ofc, you don't have to use those libraries. It's just nicer now. I previously set up my own pinvoke calls to Kernel32.dll.

- 1,976
- 10
- 15
You can use Console.SetCursorPosition
and then override everything that you need to:
public static void Main()
{
for (int i = 0; i < 100; i++)
{
Console.SetCursorPosition(0, 0);
Console.WriteLine("Index = " + i);
System.Threading.Thread.Sleep(500);
}
}
You can also create your own function to do it automatically:
public static void ClearConsole()
{
Console.SetCursorPosition(0, 0);
Console.CursorVisible = false;
for (int y = 0; y<Console.WindowHeight; y++)
Console.Write(new String(' ', Console.WindowWidth));
Console.SetCursorPosition(0, 0);
Console.CursorVisible = true;
}

- 336
- 2
- 8
-
1this might not clear the currently visible area though - if you console window is 50 lines high and your program already outputted 100 lines you would only clear the first 50 lines that are currently not visible. if you want to clear the entire buffer you need to loop from 0 to `Console.BufferHeight` instead. – Turtlefight Nov 22 '21 at 19:18
-
1@Turtlefight, good point. I would perhaps recommend hiding the cursor while clearing the screen, otherwise, you still see some weird flickers at times but else I think your answer is spot on. – jdt Nov 22 '21 at 19:24
-
1good point, hiding the cursor would probably also help :) and / or creating a large string for the entire area and then writing it out with a single `Console.Write()` to avoid flushes between the writes. But imho `Console.MoveBufferArea()` would be the best solution since it basically guarantees that whole region will be updated at once - but that'll require quite a bit of extra code to make it work :) – Turtlefight Nov 22 '21 at 19:34