How do i blink the text in console using C#?
8 Answers
Person-b was on the right track, but their code needs some changes:
static void Main()
{
string txt = "Hello, world!";
while (true)
{
WriteBlinkingText(txt, 500, true);
WriteBlinkingText(txt, 500, false);
}
}
private static void WriteBlinkingText(string text, int delay, bool visible)
{
if (visible)
Console.Write(text);
else
for (int i = 0; i < text.Length; i++)
Console.Write(" ");
Console.CursorLeft -= text.Length;
System.Threading.Thread.Sleep(delay);
}

- 20,799
- 66
- 75
- 101

- 9,739
- 7
- 47
- 51
To do this you will have to use:
console.clear
which will clear all the information in the console, but allow you to simulate a blinking text, by doing the following code you can do this: (This is in VB but its easily translated)
Dim Flash As Integer
Flash = 0
While Flash < 100 (Any number can be put here for duration)
Flash = Flash + 1
console.Backgroundcolor = consolecolor.Black
console.clear
system.threading.thread.sleep(25)
console.Backgroundcolor = consolecolor.White
console.clear
system.threading.thread.sleep(25)
End While
And that would give a blinking screen for example, for the blinking text simply adjust it:
Dim FlashWord As Integer
FlashWord = 0
While FlashWord < 100 (Any number can be put here for duration)
FlashWord = FlashWord + 1
console.Foregroundcolor = consolecolor.Black
console.clear
Console.Writeline("Hello World")
system.threading.thread.sleep(25)
console.Foregroundcolor = consolecolor.White
console.clear
Console.Writeline("Hello World")
system.threading.thread.sleep(25)
End While
And this will simulate 'flashing', the only down side is that you lose your previous on-screen info, but nothing else, and for an effect this is pretty good!

- 20,799
- 66
- 75
- 101

- 11
- 3
I improved Matthew's code a bit using \r
and some fancy use of String
:
static void Main()
{
string txt = "Hello, world!";
WriteBlinkingText(txt, 500);
}
private static void WriteBlinkingText(string text, int delay)
{
bool visible = true;
while (true)
{
Console.Write("\r" + (visible ? text : new String(' ', text.Length)));
System.Threading.Thread.Sleep(delay);
visible = !visible;
}
}
I also think that the WriteBlinkingText
method should be self-contained, so the loop is inside here, but that's just a matter of personal taste I guess :)

- 2,676
- 3
- 25
- 29
Why not use backspace and rewrite the text on the same screen line?
string blink = "Password Please!"
while (!System.Console.KeyAvailable)
{
Console.Write(blink);
Thread.Sleep(650);
for (int j = 1; j <= blink.Length + 2; j++)
{
Console.Write("\b"+(char)32+"\b");
if (j == blink.Length + 2) Thread.Sleep(650);
}
}

- 2,514
- 3
- 22
- 29
There is no direct support, you will need to overwrite the text with spaces (or in different colours) based on a timer.

- 106,783
- 21
- 203
- 265
Here is yet another function to make some text to blink
public static void blinkPrint(string str, int interval = 200, int times = 10,
ConsoleColor bgcolor = default, ConsoleColor fgcolor = default)
{
Console.BackgroundColor = bgcolor;
Console.ForegroundColor= fgcolor;
for (int i = 0; i < times; i++)
{
if (i%2 == 0)
Console.Write(str);
else
for (int j = 0; j < str.Length; j++)
Console.Write(" ");
Console.CursorLeft -= str.Length;
Thread.Sleep(interval);
}
Console.ResetColor();
if (Console.GetCursorPosition().Left != 0)
{
Console.CursorLeft -= Console.GetCursorPosition().Left;
}
for (int j = 0; j < str.Length; j++)
Console.Write(" ");
}
private void timer1_Tick(object sender, EventArgs e)//This might work better for you :)
{
Random rand = new Random();
for (int i = 0; i < 255; i++)
{
int A = rand.Next(i);
int R = rand.Next(i);
int G = rand.Next(i);
int B = rand.Next(i);
label2.ForeColor = Color.FromArgb(A, R, G, B);
}
}

- 1
string txt = "Hello, world!";
while ( doingSomething )
{
Console.Write(txt);
System.Threading.Thread.Sleep(20);
Console.CursorLeft -= txt.Length;
for ( int i = 0; i < txt.Length; i++ )
Console.Write(" ");
}
this code does not make it blink though