Timer.SynchronizingObject Property
Gets or sets the object used to marshal event-handler calls that are issued when an interval has elapsed.
Assembly: System (in System.dll)
<BrowsableAttribute(False)> <TimersDescriptionAttribute("TimerSynchronizingObject")> Public Property SynchronizingObject As ISynchronizeInvoke
Property Value
Type: System.ComponentModel.ISynchronizeInvokeThe 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.
Note |
|---|
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 property is set to the Form object.
Imports System.IO Imports System.Timers Public Class Form1 ' Create the timer to fire at a 60-second interval. Dim WithEvents timer As New System.Timers.Timer(60000) Dim sw As StreamWriter Dim hasChanged As Boolean Dim dialogIsOpen As Boolean = False Dim elapsedMinutes As Integer = 0 ' Cache the text box internally without saving it. Dim txt As String = "" Public Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load Me.Text = "Quick Text Editor" Button1.Text = "Save" TextBox1.Multiline = True ' Configure the SaveFile dialog SaveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" SaveFileDialog1.RestoreDirectory = True ' Create a timer with a 1-minute interval timer = New Timer(2000) ' Synchronize the timer with the text box timer.SynchronizingObject = Me ' Start the timer timer.AutoReset = True End Sub Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged hasChanged = True timer.Start() End Sub Friend Sub PromptForSave(sender As Object, e As ElapsedEventArgs) _ Handles timer.Elapsed If hasChanged And Not dialogIsOpen Then elapsedMinutes += 1 dialogIsOpen = True If MsgBox(String.Format("{0} minutes have elapsed since the text was saved. Save it now? ", elapsedMinutes), MsgBoxStyle.YesNoCancel Or MsgBoxStyle.Question, "Save Text") = MsgBoxResult.Yes Then If dialogIsOpen Then Button1_Click(Me, EventArgs.Empty) dialogIsOpen = False End If End If End If End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If String.IsNullOrEmpty(SaveFileDialog1.FileName) Then If SaveFileDialog1.ShowDialog() = DialogResult.OK Then sw = New StreamWriter(SaveFileDialog1.FileName, False) End If End If txt = TextBox1.Text hasChanged = False elapsedMinutes = 0 timer.Stop() End Sub Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing If sw IsNot Nothing Then sw.Write(txt) sw.Close() End If End Sub End Class
Available since 1.1

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) .