如何添加实际等待?
我目前正在尝试编写非常简单的代码。
它应该做什么:
- 输入 google
- 等待 5 秒
- 搜索一些内容。
现在,我不能做的就是等待。 有线程睡眠等,但它们停止 GUI 并使我的程序无法使用。 我也可以用计时器来做到这一点,但这不是非常有效的方法,因为在实际的应用程序中会有很多等待...... 无论如何,是否有类似的 2-3 行代码,并且不停止 GUI 的情况?
Im currently trying to code very simple thing.
what it supposed to do:
- enter google
- wait 5 seconds
- search something.
now, the part I cant do is wait.
There is thread sleep etc. but they stop GUI and makes my program unusable.
I can also do it with timers but it isnt very effective way to do it, since in actual app there will be many waits...
Are there anyway to do with it like 2-3 lines of code only, and without stopping GUI?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
计时器正是您想要的 - 您想说,“5 秒内,执行 X”(可能在 UI 线程中执行),这正是计时器为您所做的事情。如果您想将其封装在一个可以传入
Action
和TimeSpan
或其他内容的方法中,那很好 - 但计时器绝对是最佳选择。(您要使用的计时器类型取决于您希望计时器触发的线程等)
A timer is exactly what you want here - you want to say, "In 5 seconds, do X" (potentially executing in the UI thread) which is exactly what a timer does for you. If you want to encapsulate that in a single method which you can pass in an
Action
and aTimeSpan
or whatever, that's fine - but a timer is definitely the way to go.(The type of timer you want to use will depend on what thread you want the timer to fire on, etc.)
您需要在单独的线程上完成这项工作,这样就不会停止 GUI 线程。
You need to do the work on a seprate thread so it dosent halt the GUI thread.