I have built a simple console application, and I need to give a specific time for user to input a keychar.
Should I use this?
System.Threading.Thread.Sleep(1000);
For those who didn't understand, I need the program to skip the Console.ReadKey().KeyChar;
after x
seconds.
Is this possible?
I would do it like this:
DateTime beginWait = DateTime.Now;
while (!Console.KeyAvailable && DateTime.Now.Subtract(beginWait).TotalSeconds < 5)
Thread.Sleep(250);
if (!Console.KeyAvailable)
Console.WriteLine("You didn't press anything!");
else
Console.WriteLine("You pressed: {0}", Console.ReadKey().KeyChar);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With