[英]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.Timer
和System.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.Timer
和System.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.