System.Timers.Timer.SynchronizingObject Property

Gets or sets the object used to marshal event-handler calls that are issued when an interval has elapsed.

Syntax

[System.ComponentModel.Browsable(false)]
[System.ComponentModel.DefaultValue(null)]
[System.Timers.TimersDescription("The object used to marshal the event handler calls issued when an interval has elapsed.")]
public System.ComponentModel.ISynchronizeInvoke SynchronizingObject { get; set; }

Value

Documentation for this section has not yet been entered.

Remarks

When Timer.SynchronizingObject is null, the method that handles the Timer.Elapsed event is called on a thread from the system-thread pool. For more information on system-thread pools, see System.Threading.ThreadPool.

When the Timer.Elapsed event is handled by a visual Windows Forms component, such as a button, accessing the component through the system-thread pool might result in an exception or just might not work. Avoid this effect by setting Timer.SynchronizingObject to a Windows Forms component, which causes the method that handles the Timer.Elapsed event to be called on the same thread that the component was created on.

Note:

Even if the Timer.SynchronizingObject property is not null, Timer.Elapsed events can occur after the erload:System.Timers.Timer.Dispose or Timer.Stop method has been called or after the Timer.Enabled property has been set to false, because the signal to raise the Timer.Elapsed event is always queued for execution on a thread pool thread. One way to resolve this race condition is to set a flag that tells the event handler for the Timer.Elapsed event to ignore subsequent events.

If the System.Timers.Timer is used inside Visual Studio in a Windows Forms designer, Timer.SynchronizingObject is automatically set to the control that contains the System.Timers.Timer. For example, if you place a System.Timers.Timer on a designer for Form1 (which inherits from System.Windows.Forms.Form), the Timer.SynchronizingObject property of System.Timers.Timer is set to the instance of Form1.

Requirements

Namespace: System.Timers
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0