Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console.ReadLine Break

Tags:

c#

sleep

console

I'm trying to figure out how I can use Console.ReadLine and a timer. My console program is designed to run a long process automatically, with this process restarting every 30 seconds after the previous process completed. I want to give the user the ability to break the auto-run by typing a command though. If I use Console.ReadLine() though, it will wait until the user enters something, whereas I want the program to continue on through a loop if nothing is entered within 30 seconds. . . Any thoughts??

For example:

RunProcess > Wait 30s for User Input. If none: Continue Loop

Thanks a lot!

like image 685
keynesiancross Avatar asked Jun 01 '26 18:06

keynesiancross


1 Answers

You could run your timer on a separate thread. When the user enters text, store it in a variable that is accessible to both threads. When the timer ticks, see if anything is entered and continue accordingly.

Be sure to be thread safe :-)

EDIT:

You can use a System.Threading.Timer to tick every 30 seconds and in its callback method, check if the text has been set.

like image 74
Mark Avenius Avatar answered Jun 04 '26 09:06

Mark Avenius