Console.ReadLine Break [英] Console.ReadLine Break
问题描述
我想弄清楚如何使用Console.ReadLine和一个计时器。我的控制台程序被设计为自动运行一个长过程,此过程在上一个过程完成后每30秒重新启动一次。我想给用户打破自动运行的能力,通过键入命令。如果我使用Console.ReadLine()虽然,它会等待,直到用户输入的东西,而我想让程序继续通过一个循环,如果在30秒内没有输入。 。 。任何想法
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??
例如:
RunProcess>输入。如果没有:继续循环
RunProcess > Wait 30s for User Input. If none: Continue Loop
非常感谢。
推荐答案
您可以在单独的线程上运行您的计时器。当用户输入文本时,将其存储在两个线程都可访问的变量中。
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 :-)
编辑:
您可以使用 System.Threading.Timer
每隔30秒打勾一次,在其回调方法中,检查如果文本已设置。
You can use a System.Threading.Timer
to tick every 30 seconds and in its callback method, check if the text has been set.
这篇关于Console.ReadLine Break的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!