定时器和多线程

发布于 2024-10-09 13:32:26 字数 125 浏览 0 评论 0原文

我有一个关于计时器和线程的问题。我注意到计时器在线程内启动时行为异常,而计时器是 Winform 的一部分。

一般来说,我对与线程和计时器相关的问题感兴趣。

祝大家新年快乐,答案可能要等到2011年了:)

I have a question about timers and threads. I noticed that the timers misbehaving when started within the threads, while the timers are part of the Winform.

Generally I'm interested in problems related threads and timers.

Happy New Year to you all, the answers may be to wait until 2011:)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

笙痞 2024-10-16 13:32:26

听起来您正在使用 System.Threading.Timer 并使用执行 GUI 更新的 TimerCallback 。是这样吗?

有许多正确的方法可以解决这个问题。如果您希望更新 UI,请使用 System.Windows.Forms.Timer 并处理其 Tick 事件。使用 BackgroundWorker,在其 DoWork 事件中执行非 UI 工作,然后在其 RunWorkerCompleted 事件中执行 UI 更新(如果您执行长时间操作)运行后台任务。

一般来说,了解与 Windows 窗体相关的多线程的重要一点是:所有 Windows 窗体应用程序都有一个 UI 线程,这是唯一允许执行 UI 更新的线程。它不断处理一个队列,用户操作通过事件推送到该队列并进行处理。当您尝试从该线程之外的任何线程执行任何更新 UI 控件的操作时,您会收到异常,因为 Windows 窗体组件的设计中没有计划此行为,因此很可能会导致错误或可能导致整个应用程序崩溃。

因此,多线程的方法通常是将工作分成两部分,一部分可以在后台完成(在非 UI 线程上),一部分必须发送到正在由 UI 线程处理的队列中以便可以处理以安全的方式。 System.Windows.Forms.TimerBackgroundWorker 等类型的用处在于,它们为您封装了此过程中的许多困难细节,使您能够专注于代码你想跑。

这是多线程如何与 Windows 窗体配合使用的高级视图。我确信其他人可以提供大量参考资料,为您提供有关该主题的更多信息(如果没有其他人这样做,也许我可以稍后查找一些)。

Sounds like you're using a System.Threading.Timer and using a TimerCallback that performs GUI updates. Is that it?

There are a number of correct ways to deal with this. Use a System.Windows.Forms.Timer and handle its Tick event if you're looking to update the UI. Use a BackgroundWorker, do non-UI work in its DoWork event and then perform UI updates in its RunWorkerCompleted event if you're performing long-running background tasks.

In general, the important thing to understand about multithreading as it pertains to Windows Forms is this: all Windows Forms application have a UI thread, which is the only thread that is allowed to perform UI updates. It is continually processing a queue onto which user actions are pushed and handle via events. When you try to do anything that updates a UI control from any thread besides this thread, you get an exception because this behavior was not planned for in the design of Windows Forms components, and would therefore very likely cause bugs or possibly crash the entire application.

So the approach to multithreading is generally to separate work into two parts, that which can be done in the background (on a non-UI thread) and that which must sent to the queue being processed by the UI thread so that it can be handled in a safe manner. The usefulness of types like System.Windows.Forms.Timer and BackgroundWorker is that they encapsulate many of the difficult details of this process for you, allowing you to focus on the code you want to run.

That's a high level view of how multithreading works with Windows Forms. I'm sure others can provide plenty of references pointing you to more information on the subject (and if nobody else does, maybe I can look some up later).

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