11
        timer.Interval = 5000;
        timer.Tick += new EventHandler(timer_Tick);
        timer.Start();

Does "timer_Tick" method start in a new thread or is it still in the thread it was created in?

DOK
  • 32,337
  • 7
  • 60
  • 92
syncis
  • 1,395
  • 4
  • 25
  • 43

2 Answers2

10

No, a timer runs in the thread in which it was created.

I'm assuming you are talking about System.Windows.Forms.Timer which is implemented using the thread message loop. Underlying a WinForms timer is the Win32 API SetTimer() which operates by posting WM_TIMER messages to the message queue of the thread which SetTimer().

One of the consequences of this is that if you have an event handler that takes longer than your timer interval then your timer will not fire at the desired interval. If this was a problem then you'd need to house your timer in another thread.

As a thought experiment, imagine what would happen if your timer event did execute in a different thread. Now you have a synchronisation problem to handle. Your timer event is likely to want to access objects from the other thread. But to do so will result in race conditions.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    Yup. Could be DispatcherTimer too (WPF). – Hans Passant Mar 05 '11 at 16:32
  • I think this answer conflicts with the answers in this question: http://stackoverflow.com/questions/1435876/do-c-sharp-timers-elapse-on-a-separate-thread The answers in that question include sample code which demonstrate the manner in which you might answer this question with simple code. – Owen Ivory Mar 29 '17 at 16:37
  • @Owen No. I stated in the answer which timer I was referring to. – David Heffernan Mar 29 '17 at 16:40
  • David, You are correct. Sadly c# defines 4 different Timer classes, most of which are designed for multi-threading. System.Windows.Forms.Timer is designed as a single threaded object. https://msdn.microsoft.com/en-us/library/system.threading.timer(v=vs.110).aspx describes more about the different timers, and clearly specifies that System.Windows.Forms.Timer is designed as single thread. – Owen Ivory Mar 29 '17 at 20:31
8

A timer doesn't really "run". That is, when you start a timer, the operating system creates some data structures that tell it to issue a "tick" periodically--at whatever period you've specified. But it's not like the timer is sitting there spinning, eating CPU resources while it waits for the proper time. All of the .NET timer types and the Windows API timer types work this way.

The difference is in what happens when it comes time to make a tick. As @David Hefferman pointed out, with System.Windows.Forms.Timer, the Elapsed event handler is called on the same thread that created the timer. System.Threading.Timer calls its callback on a thread pool thread. Under the hood, System.Timers.Timer is called on a pool thread, but you can use the SynchronizingObject property to raise the Elapsed event on the UI thread or any other thread.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351