I'm building a c# console app (.NET framework) and i want to make a nice looking app by using some "animations". I want to print "Press any Key to continue ..." and have it flashing ( appear then disappear until the user actually presses any key.
do
{
while (!Console.KeyAvailable)
{
Console.WriteLine("Press any key to continue...");
/* here i want the program to wait for like 500ms,
clear console and wait 500ms again before rewriting line*/
Console.Clear();
}
} while (Console.ReadKey(true).Key != ConsoleKey.Escape);
A slightly different approach would be to use the Stopwatch
class to measure time, and only change the text if we've passed the specified "flash interval".
We could write a method to do this, which would take in a string prompt
to display, and a TimeSpan interval
that specifies how long to wait between flashing the text.
In the code, we would capture the cursor position and console colors, start a stopwatch, and then every time the stopwatch passes the amount of time specified by interval
, we would swap the Console.ForegroundColor
and Console.BackgroundColor
.
The method would do this until the user presses a key, which we would return back to the caller:
private static ConsoleKey FlashPrompt(string prompt, TimeSpan interval)
{
// Capture the cursor position and console colors
var cursorTop = Console.CursorTop;
var colorOne = Console.ForegroundColor;
var colorTwo = Console.BackgroundColor;
// Use a stopwatch to measure time interval
var stopwach = Stopwatch.StartNew();
var lastValue = TimeSpan.Zero;
// Write the initial prompt
Console.Write(prompt);
while (!Console.KeyAvailable)
{
var currentValue = stopwach.Elapsed;
// Only update text with new color if it's time to change the color
if (currentValue - lastValue < interval) continue;
// Capture the current value, swap the colors, and re-write our prompt
lastValue = currentValue;
Console.ForegroundColor = Console.ForegroundColor == colorOne
? colorTwo : colorOne;
Console.BackgroundColor = Console.BackgroundColor == colorOne
? colorTwo : colorOne;
Console.SetCursorPosition(0, cursorTop);
Console.Write(prompt);
}
// Reset colors to where they were when this method was called
Console.ForegroundColor = colorOne;
Console.BackgroundColor = colorTwo;
return Console.ReadKey(true).Key;
}
Now, on the calling side, we would pass it the text "Press escape to continue" and the amount of time we want to wait (TimeSpan.FromMilliseconds(500)
in your case), and then we could call this in an infinite while
loop, until the user presses ConsoleKey.Escape
:
private static void Main()
{
// Flash prompt until user presses escape
while (FlashPrompt("Press escape to continue...",
TimeSpan.FromMilliseconds(500)) != ConsoleKey.Escape) ;
// Code execution continues after they press escape...
}
The nice thing here is that you can re-use the logic and can specify shorter or longer flash times. You can also change the colors that get "flashed" by specifying them before calling the method (or the method could be written to take them as arguments).
For example, try this out:
private static void Main()
{
Console.WriteLine("Hello! The text below will flash red " +
"and green once per second until you press [Enter]");
Console.ForegroundColor = ConsoleColor.Red;
Console.BackgroundColor = ConsoleColor.Green;
while (FlashPrompt("Press [Enter] to continue...",
TimeSpan.FromSeconds(1)) != ConsoleKey.Enter) ;
Console.ResetColor();
// Code will now continue in the original colors
}