start a timer from different thread in c#

46,723

Solution 1


You could try to start the timer this way:

Add in form constructor this:

System.Timers.Timer aTimer = new System.Timers.Timer();
 aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
 // Set the Interval to 1 second.
 aTimer.Interval = 1000;

Add this method to Form1:

 private static void OnTimedEvent(object source, ElapsedEventArgs e)
 {
   //do something with the timer
 }

On button click event add this:

aTimer.Enabled = true;

This timer is already threaded so no need to start a new thread.

Solution 2

It is true what Matías Fidemraizer says. But, there is a work around...

When you have a control on your form that is invokable (eg. a statusbar), just invoke that one!

C# Code sample:

private void Form1_Load(object sender, EventArgs e)
{
    Thread sampleThread = new Thread(delegate()
    {
        // Invoke your control like this
        this.statusStrip1.Invoke(new MethodInvoker(delegate()
        {
            timer1.Start();
        }));
    });
    sampleThread.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
    MessageBox.Show("I just ticked!");
}

Solution 3

System.Windows.Forms.Timer works in a single-threaded application.

Check this link:

Remarks says:

A Timer is used to raise an event at user-defined intervals. This Windows timer is designed for a single-threaded environment where UI threads are used to perform processing. It requires that the user code have a UI message pump available and always operate from the same thread, or marshal the call onto another thread.

Read more "Remarks" section and you'll find that Microsoft recommends that you use this timer synchronizing it with the UI thread.

Share:
46,723
Swati
Author by

Swati

http://swati-the-techical-side.blogspot.in/

Updated on June 06, 2020

Comments

  • Swati
    Swati almost 4 years

    Hi i have stepped into some problem related to timer. hope somebody can help..

    1. I have a windows form containing a button
    2. when i click on that button i start a parameterised thread
    Thread thread1 = new Thread(new ParameterizedThreadStart( execute2));
    thread1.Start(externalFileParams);
    
    1. the code inside the thread executes very well
    2. at the last line of this thread i start a timer

    .

    public void execute2(Object ob)
    {
        if (ob is ExternalFileParams)
        {
            if (boolean_variable== true)
              executeMyMethod();//this also executes very well if condition is true
            else
            {
                timer1.enabled = true;
                timer1.start();
                }
            }
        }
    }
    

    5 but the tick event of the timer is not fired

    I am working on VS2008 3.5 framework. I have dragged the timer from toolbox and set its Interval to 300 also tried to set Enabled true/false method is timer1_Tick(Object sender , EventArgs e) but its not fired

    can anybody suggest what I am doing wrong?

  • Matías Fidemraizer
    Matías Fidemraizer about 13 years
    Haha, read my last sentence (it was there for an hour!!) :) Thank you anyway!