开发者

Does the System.Windows.Forms.Timer run on a different thread than the UI?

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 al开发者_运维问答so 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?


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.


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.


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.


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


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.


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();
    }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜