0

i'm a beginner programmer and i was wondering if there was a way to do this, ideally i would have it so that it only asks for the input for 5 seconds, cancels and moves on if nothing has been pressed would i need a separate thread?, is there even a way to do something like this in c#? or is this just a limitation of using the console?

1 Answers1

0

You could use

string input = string.Empty;
var task = Task.Run(() => input = Console.ReadLine());
if(task.Wait(TimeSpan.FromSeconds(5)))
{
    Console.WriteLine($"Value Read {input}");
}
else
{
    Console.WriteLine("You are timed out");
}

Task.Wait waits for the specified TimeSpan duration for the task to be completed, before timing out.

Anu Viswan
  • 17,797
  • 2
  • 22
  • 51