Does System.Timers.Timer use a separate thread to execute the timer_elapsed method

I am using an instance of System.Timers.Timer in a windows service. My question is, all that was in his Timer_Elapsed method, does this code execute in the same thread where the Windows service was started, or does the timer internally execute the expired method in a separate thread?

+3
source share
3 answers

The last blog post from Abhishek Sur may answer your question.

So System.Timers.Timer:

  • It calls an available thread in ThreadPool to start the interface.
  • ThreadSafe, each event handler has explicit locks.
  • It can be launched in any thread using ISynchronizeObject.
+4
source

, , (. MSDN):

SynchronizingObject null, Elapsed ThreadPool. Elapsed Interval, ThreadPool. .

; , .

+1

Elapsed , , , .

This is primarily intended for Windows Forms applications, where System.Windows.Forms.Control(and all derived types, such as Form) implement an interface ISynchronizeInvokeand can be easily tied to a timer to synchronize execution. In the Windows Service environment, you will have to program an implementation of a stub for this interface, which performs the necessary synchronization of threads, since the service itself does not provide such an implementation.

0
source

All Articles