26

I have a main thread that creates a form object which creates and sets a timer to run a function named updateStatus() every minute. But updateStatus() is also called by the main thread at several places.

However, I am not clear about whether or not it will cause any synchronization problems. Does the System.Windows.Forms.Timer in C# run on a different thread other than the main thread?

0

6 Answers 6

26

No, the timer events are raised on the UI thread.

You won't have any synchronicity problems. This is the correct version of the timer control to use in a WinForms application; it's specifically designed to do what you're asking. It's implemented under the hood using a standard Windows timer.

The documentation confirms this in the Remarks section:

A Timer is used to raise an event at user-defined intervals. This Windows timer is designed for a single-threaded environment where UI threads are used to perform processing. It requires that the user code have a UI message pump available and always operate from the same thread, or marshal the call onto another thread.

When you use this timer, use the Tick event to perform a polling operation or to display a splash screen for a specified period of time. Whenever the Enabled property is set to true and the Interval property is greater than zero, the Tick event is raised at intervals based on the Interval property setting.

2
  • So the windows timer is running on the main thread, it will be always synchronous and I need not put lock block inside the updateStatus() function . Am I right?
    – user186246
    Commented Apr 18, 2011 at 4:41
  • @user: Yes, that's correct. It runs just like any other event that gets raised on your form, such as the Paint or Load events. You don't need to lock in those handlers, either.
    – Cody Gray
    Commented Apr 18, 2011 at 4:45
9

No, the timer's Tick event is raised from the UI thread by the message loop when it receives a WM_TIMER message. You're always good with that, it can only run when your UI thread is idle.

0
5

No.

The whole point of a Windows.Forms Timer is that it runs on the GUI Thread.

Windows (WinForms) runs something called the MessagePump (see Application.Run()) and this is what makes the Timer possible.

All your code runs as part of an Eventhandler somehow, and a Timer tick will never 'interrupt' any other event handler.

1
  • Hi, Iam not aware of messagePump . Can you please give some information on it?
    – user186246
    Commented Apr 18, 2011 at 4:44
4

The Windows.Forms timer raises the event back on the UI thread, presumably via the sync-context.

If you want a non-UI thread, there are other timers - for example System.Timers.Timer or System.Threading.Timer

1
  • 2
    Not quite. It uses windows messages, so it never leaves the UI thread.
    – SLaks
    Commented Apr 17, 2011 at 15:49
2

According to the documentation it runs on the main UI thread:

A Timer is used to raise an event at user-defined intervals. This Windows timer is designed for a single-threaded environment where UI threads are used to perform processing. It requires that the user code have a UI message pump available and always operate from the same thread, or marshal the call onto another thread.

0
-1

I don't want to impeach the reputation of others how has answered, that Windows.Forms.Timer does always run on GUI thread, but I have to contradict. :)

I would answer - it depends. It can be, that Windows.Forms.Timer does run on Non-GUI thread!

Windows.Forms.Timer does fire it's Timer.Tick event on the thread, where the Form, which contains the timer, has been created.

If the form is created on the GUI thread, then the Timer.Tick runs on GUI thread, if however Form is created on the e.g. some thread XXX from thread pool(what you should avoid), then the Timer.Tick runs on this XXX thread.

Task.Run(()=>
{
    using (var formWithTimer = new SomeFormWithTimer()) //Form created on worker thread, Tick handled on it.
    {
        formWithTimer.ShowDialog();
    }
});
3
  • @HenkHolterman Do you mean, that my answer is wrong?
    – Rekshino
    Commented Sep 27, 2022 at 20:50
  • @HenkHolterman or do you agree, that Timer.Tick can under circumstances run on Non-GUI thread ;)?
    – Rekshino
    Commented Sep 27, 2022 at 20:54
  • It's not my problem, in answer it's also addressed, that it shouldn't be done. The answer is here for the complement to emphasize, that Timer.Tick not mandatory runs on GUI thread, even if it's not a realistic scenario.
    – Rekshino
    Commented Sep 29, 2022 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.