在 VB.NET 中,等待计时器或按键
我有一个控制台应用程序,旨在继续运行直到它被终止。本质上:
<setupCode>
Do
<doProcessing>
Thread.Sleep(<interval>)
Loop
显然,本质上这将继续运行,直到用户杀死它。
我想做的是将 Thread.Sleep 调用替换为等待两个单独事件之一的等待条件...类似
WaitFor(<intervalPasses> Or <KeyPress>)
这样,应用程序基本上只会在后台休眠直到下一个间隔过去,但用户可以通过按键“唤醒它”。我还希望能够获取有关按键的信息,以便例如,如果他们按下 Enter,我可以开始下一次迭代,而无需等待计时器,如果他们按下 Escape,我将完全退出循环,如果他们按其他任何内容我会继续等待计时器或这两个键之一。
(等待计时器)
或 (等待按键)
都很容易。
等待(计时器或按键)
我不知道该怎么做。
提前致谢。
I have a console application which is intended to just keep running until it is killed. Essentially:
<setupCode>
Do
<doProcessing>
Thread.Sleep(<interval>)
Loop
Essentially this will keep running until the user kills it, obviously.
What I'd like to do is replace the Thread.Sleep
call with a wait-condition waiting on either one of two separate events... something like
WaitFor(<intervalPasses> Or <KeyPress>)
so that the app will basically just sleep in the background until the next interval passes, but so that the user can "wake it up" with a keypress. I also want to be able to get the information about the keypress so that e.g. if they pressed Enter I can just start the next iteration without waiting for the timer and if they pressed Escape i would exit the loop altogether, and if they press anything else I would continue waiting for either the timer or one of those two keys.
Either (waiting for a timer)
OR (waiting for a keypress)
would be easy.
Waiting for either (a timer OR a keypress)
I am not sure how to do.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是 Windows 窗体和 WPF 的工作方式。我强烈建议您使用其中之一来实现此任务。使用计时器,实现其 Tick 事件。并实现表单的KeyDown事件。类似这样,你的问题太笼统,无法更具体。
This is how Windows Forms and WPF work. I'd strongly recommend you use one of them to implement this task. Use a Timer, implement its Tick event. And implement the form's KeyDown event. Something like that, your question is too generic to be more specific.