简体   繁体   English

计时器的回调线程

[英]Timer's callback thread

I was thinking about how timers work, and there's something I don't fully understand. 我在考虑计时器的工作原理,有些事情我还不太了解。
A Timer uses an event that is called when the time elapses. Timer使用时间流逝时调用的事件。 This I assume is triggered by an OS event that controls the clock (or is controlled by the clock). 我认为这是由控制时钟(或由时钟控制)的OS事件触发的。
But I'm not sure what thread will catch that event.. 但是我不确定哪个线程会捕获该事件。

Does a Timer object have a thread of its own ? Timer对象是否具有自己的线程?
Or will the event be called in the thread which created the timer object ? 还是在创建计时器对象的线程中调用事件?

EDIT: 编辑:

And is there a difference in that aspect between System.Timers.Timer and System.Windows.Forms.Timer ? System.Timers.TimerSystem.Windows.Forms.Timer之间在这方面是否有区别?

It depends on the Timer . 这取决于Timer If you use System.Windows.Forms.Timer , the tick event will be handled in the UI thread. 如果使用System.Windows.Forms.Timer ,则tick事件将在UI线程中处理。 In System.Threading.Timer and System.Timers.Timer it defaults to a thread pool thread. System.Threading.TimerSystem.Timers.Timer它默认为线程池线程。 However, in the latter case you can specify a SynchronizingObject which can be used to marshal the handler to an appropriate thread. 但是,在后一种情况下,您可以指定一个SynchronizingObject ,该对象可用于将处理程序编组到适当的线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM