Lets say I have a Timer
which has the Elapsed
event handled. What thread is the method code executed on?
Is is the thread that the timer was created on? If so, that means if I wish it to be on another thread I have to ensure that the timer gets created in the threads main loop rather than the initialization of the thread - correct?
The thread is one out of the threadpool. There is no way to prodict which thread will be associated with the elapsed method.
Details: http://msdn.microsoft.com/en-us/library/system.timers.timer.elapsed.aspx
(And it should not matter, from a designs perspective)
Edit: Actually there is a method to define which thread is used; You can used the SynchronizingObject property of the timer:
When SynchronizingObject is null, the method that handles the Elapsed event is called on a thread from the system-thread pool. For more information on system-thread pools, see ThreadPool.
When the Elapsed event is handled by a visual Windows Forms component, such as a button, accessing the component through the system-thread pool might result in an exception or just might not work. Avoid this effect by setting SynchronizingObject to a Windows Forms component, which causes the method that handles the Elapsed event to be called on the same thread that the component was created on.
See: http://msdn.microsoft.com/en-us/library/system.timers.timer.synchronizingobject.aspx
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.