none
System.Windows.Forms.Timer doesn't tick when fired by System.Timers.Timer's event RRS feed

  • Question

  • My program have two different timer.

    The one is System.Windows.Forms.Timer(tf) and another is System.Timers.Timer(tt).

    tf is ticked normally when started from Form_Load.

    However it is not fired when ticked from tt.

    I need to use two different type of timers.

    How to solve this issue?

    I made sample program below.

    public partial class Form1 : Form
        {
            System.Timers.Timer tt;
            System.Windows.Forms.Timer tf;
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                tt = new System.Timers.Timer();
                tt.AutoReset = false;
                tt.Interval = 5000;
                tt.Elapsed += OnTimedEvent;
                tt.AutoReset = false;
                tt.Enabled = true;
                tt.Start(); 
            }
            private void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
            {
                tf = new System.Windows.Forms.Timer();
                tf.Tick += new EventHandler(TimerEventProcessor);
    
                // Sets the timer interval to 5 seconds.
                tf.Interval = 5000;
                tf.Start();
            }
            private void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
            {
                tf.Stop();
                MessageBox.Show("TimerEventProcessor was invoked!");
            }
        }



    • Edited by Jeff0803 Sunday, July 7, 2019 6:18 AM
    Sunday, July 7, 2019 3:50 AM

Answers

  • I suspect that the System.Windows.Forms.Timer might be subject to the same limitation as other Forrms controls, which cannot be manipulated from a Thread that is different than the one that created the forms. Since the System.Timers.Timer fires its Elapsed event in a different thread, it would incur in this limitation.

    You can solve it by marshalling execution into the main thread using the Invoke method of a control (including the form itself):

            private void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
            {
              this.Invoke(new MethodInvoker(() => {
                tf = new System.Windows.Forms.Timer();
                tf.Tick += new EventHandler(TimerEventProcessor);
    
                // Sets the timer interval to 5 seconds.
                tf.Interval = 5000;
                tf.Start();
              });
            }

    Sunday, July 7, 2019 12:26 PM
    Moderator
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer. So that it will help other members to find the solution quickly if they face the similar issue.

    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.

    • Marked as answer by Jeff0803 Thursday, July 18, 2019 1:24 PM
    Thursday, July 11, 2019 5:43 AM
    Moderator

All replies

  • I suspect that the System.Windows.Forms.Timer might be subject to the same limitation as other Forrms controls, which cannot be manipulated from a Thread that is different than the one that created the forms. Since the System.Timers.Timer fires its Elapsed event in a different thread, it would incur in this limitation.

    You can solve it by marshalling execution into the main thread using the Invoke method of a control (including the form itself):

            private void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
            {
              this.Invoke(new MethodInvoker(() => {
                tf = new System.Windows.Forms.Timer();
                tf.Tick += new EventHandler(TimerEventProcessor);
    
                // Sets the timer interval to 5 seconds.
                tf.Interval = 5000;
                tf.Start();
              });
            }

    Sunday, July 7, 2019 12:26 PM
    Moderator
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer. So that it will help other members to find the solution quickly if they face the similar issue.

    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.

    • Marked as answer by Jeff0803 Thursday, July 18, 2019 1:24 PM
    Thursday, July 11, 2019 5:43 AM
    Moderator