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

后端 未结 5 1548
眼角桃花
眼角桃花 2020-12-01 14:24

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

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-01 14:43

    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.

提交回复
热议问题