none
When the System.Timers.Timer.SynchronizingObject Property should be used? RRS feed

  • Question

  • I don't understand when the System.Timers.Timer.SynchronizingObject property is used.

    Can anybody show me when it should be used with sample code?

    With and without it, what is the difference?

    Thursday, July 18, 2019 5:26 PM

All replies

  • In my opinion, the event of System.Timers.Timer, in contrast with System.Windows.Forms.Timer, happens in a separate thread which is different from the form’s thread. This makes difficult accessing the form’s controls, and you have to use Invoke or BeginInvoke. (Otherwise the operations do not work or you receive a “Cross-thread operation is not valid” error).

    But if you execute ‘myTimer.SynchronizingObject = f’ or ‘myTimer.SynchronizingObject = this’, where f or this is the form, then the Elapsed event of the timer is executed in the thread of the form. Thus, you can access the controls directly. (Note that the form will be blocked during this event, therefore the handler should not perform lengthy operations).

    An example is shown in documentation: https://docs.microsoft.com/en-us/dotnet/api/system.timers.timer.synchronizingobject. Since SynchronizingObject is set, it can access the text of textBox1.

    • Edited by Viorel_MVP Thursday, July 18, 2019 6:25 PM
    Thursday, July 18, 2019 6:22 PM
  • Hi Jeff0803,

    Thank you for posting here.

    For your question, you want to understand the usage of SynchronizingObject property.

    Here is a simple code for your reference.

       using Timers = System.Timers;
    
     public partial class Form1 : Form
        {
            bool hasChanged = false;
            public Form1()
            {
                InitializeComponent();
            }
            Timers.Timer timer = new Timers.Timer();
            private void Form1_Load(object sender, EventArgs e)
            {
    
                timer.SynchronizingObject = this.textBox1;
                timer.Interval = 100;
                timer.Elapsed += Timer_Elapsed;  
            }
            int i = 0;
            private void Timer_Elapsed(object sender, Timers.ElapsedEventArgs e)
            {
    
                textBox1.Text = i++.ToString();
            }
    
            private void Button1_Click(object sender, EventArgs e)
            {
                timer.Stop();
            }
    
            private void TextBox1_TextChanged(object sender, EventArgs e)
            {
                hasChanged = true;
                timer.Start();
            }
        }

    You could look at the following picture, if we don't set it, it will throw an exception.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 19, 2019 5:40 AM
    Moderator