I need to ask for input in console and wait for few sec may be a min after that I need to default to some value. How can basically time our on console input in c# and proceed with default settings? If you have sample code that would be great.
You can wait for input in C++ by calling the cin::get() function, which extracts a single character or optionally multiple characters from the input stream. Basically, ::get() function blocks the program execution until the user provides input and specifically n character to indicate the end of the input.
getch() is a nonstandard function and is present in conio. h header file which is mostly used by MS-DOS compilers like Turbo C. It is not part of the C standard library or ISO C, nor is it defined by POSIX. Like these functions, getch() also reads a single character from the keyboard.
We should use "%[^\n]", which tells scanf() to take input string till user presses enter or return key. Scanf scans till user presses enter or till user presses return key.
In C programming, scanf() is one of the commonly used function to take input from the user.
You could use the timer object in System.Timers.Timer and set it to 60 secs, enable it and if someone enters something into the console just disable it, if not then just handle the what happens in the Timer.Elapsed event.
static void Main(string[] args)
{
System.Timers.Timer timer = new System.Timers.Timer(60000);
timer.Elapsed += new System.Timers.ElapsedEventHandler(T_Elapsed);
timer.Start();
var i = Console.ReadLine();
if (string.IsNullOrEmpty(i))
{
timer.Stop();
Console.WriteLine("{0}", i);
}
}
static void T_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Console.WriteLine("Defult Values Used");
var T = (Timer)sender;
T.Stop;
}
Not sure if this is the best way. I have tested this but as I said it may not be the best way.
See here, it uses a nice little polling technique on the console, which although a bit crude, very effective.
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