在 VB.NET 中,等待计时器或按键

发布于 2024-09-15 20:26:35 字数 578 浏览 9 评论 0原文

我有一个控制台应用程序,旨在继续运行直到它被终止。本质上:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫瑾少女 2024-09-22 20:26:35

这就是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文