Timer.SynchronizingObject Property

 

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

Namespace:   System.Timers
Assembly:  System (in System.dll)

[<BrowsableAttribute(false)>]
[<TimersDescriptionAttribute("TimerSynchronizingObject")>]
member SynchronizingObject : ISynchronizeInvoke with get, set

Property Value

Type: System.ComponentModel.ISynchronizeInvoke

The ISynchronizeInvoke representing the object used to marshal the event-handler calls that are issued when an interval has elapsed. The default is null.

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

When the 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 SynchronizingObject to a Windows Forms component, which causes the method that handles the Elapsed event to be called on the same thread that the component was created on.

System_CAPS_noteNote

Even if the SynchronizingObject property is not null, Elapsed events can occur after the Dispose or Stop method has been called or after the Enabled property has been set to false, because the signal to raise the 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 Elapsed event to ignore subsequent events.

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

The following example is a Windows Forms app that serves as a very simple text file editor. When the text in the text box has not been saved, the app asks the user at one-minute intervals whether he or she wants to save the contents of the text box. To do this, the Interval property is set to one minute (60,000 milliseconds), and the SynchronizingObject

No code example is currently available or this language may not be supported.

The example requires that you add the following controls to the form:

  • A TextBox control named TextBox1 (its default name).

  • A Button control named Button1 (its default name).

  • A SaveFileDialog control named SaveSaveFileDialog1 (its default name) .

.NET Framework
Available since 1.1
Return to top
Show: