简体   繁体   中英

Timer doesn't want to start again after it was disabled

I am writing a simple C# program that attempts to do something every x amount of seconds using System.Forms.Timer

The tick event calls a method that starts a new thread and disables the timer, then when the thread is done with its work, it enables the timer again, but the problem is, now it doesn't tick after it's been enabled.

static System.Windows.Forms.Timer testtimer = new System.Windows.Forms.Timer();

    static void Main()
    {
        testtimer.Tick += testtimertick;
        testtimer.Interval = 5000;
        testtimer.Enabled = true;
        testtimer.Start();

        while (true)
        {
            Application.DoEvents();  //Prevents application from exiting
        }

    }

 private static void testtimertick(object sender, System.EventArgs e)
    { 

            testtimer.Enabled = false;
            Thread t = new Thread(dostuff);
            t.Start();
    }


private static void dostuff()
    {

       //Executes some code
       testtimer.Enabled = true; //Re enables the timer but it doesn't work
       testtimer.Start();
     }

As @grzenio said , it appears that your issue has to do with the fact that you are making cross thread calls to a Windows Form Control that was created on a different thread.

If you are using .NET 4.5 (C# 5.0), I would suggest looking at the async/await keywords , a good introduction can be found at Stephen Cleary's Blog

An example of how you could use async and await with your legacy "DoStuff":

    private async void _Timer_Tick(object sender, EventArgs e)
    {
        _Timer.Enabled = false;

        await Task.Run((() => DoStuff()));

        _Timer.Enabled = true;
    }

Things to notice:

  1. async was added to the Timer_Tick event's signature.
  2. The await keyword along with Task.Run was used to asynchronously run the DoStuff.

When using these keywords, the DoStuff will be run asynchronously and once DoStuff returns, it will continue on the line after await using the context of the thread that originally called Tick.

Don't use a GUI timer without a GUI. Don't spin with DoEvents because you are burning 100% of a CPU core with that. Use a System.Threading.Timer . It will just work.

You can use System.Threading.Timer to do what you want to do, using the Change Method to set the time and the Period, Just restart it when you finish your work.

class Program
{
    static System.Threading.Timer testtimer;
    static void Main(string[] args)
    {
        testtimer = new System.Threading.Timer(testtimertick);
        testtimer.Change(5000,0);

        Console.ReadLine();
    }

    private static void testtimertick(object sender)
    {

        Thread t = new Thread(dostuff);
        t.Start();
    }


    private static void dostuff()
    {

        //Executes some code
        Thread.Sleep(2000);
        Console.WriteLine("Tick");
        testtimer.Change(5000, 0);
    }
}

Windows窗体控件不是线程安全的,您应该确保在UI线程上使用它们,请参阅例如C#Windows窗体应用程序 - 从另一个线程AND类更新GUI?

static System.Windows.Forms.Timer testtimer = new System.Windows.Forms.Timer();

    static void Main()
    {
        testtimer.Tick += testtimertick;
        testtimer.Interval = 5000;
        testtimer.Enabled = true;

        while (true)
        {
            Application.DoEvents();  //Prevents application from exiting
        }

    }

 private static void testtimertick(object sender, System.EventArgs e)
    { 

            Thread t = new Thread(dostuff);
            t.Start();
    }


private static void dostuff()
    {
       testtimer.Enabled = false;
       //Executes some code
       testtimer.Enabled = true; //Re enables the timer but it doesn't work
       testtimer.Start();
     }

I had a similar issue just now. I was disabling the timer and enabling again whenever I want. The next time when I enable, it won't work.

I tried disposing the Timer object when I want to disable and creating new instance of it each time I want to enable it. Didn't work though.

Figured out a solution then. I'm removing the event which is configured in testtimer.Tick, and then adding it back when I want to enable the timer.

So the timer internally will be always instantiated with valid values and have its property Enabled = true. The only difference is that it won't have anything actually to perform whenever a tick event triggers.

This would imitate disabling and enabling the timer and makes it working as good as you control like Enabled = false / true.

If you really want to stick to the GUI timer, and start it from non UI thread, you can try to do similar stuff, then write to GUI from non UI thread.

Not the ideal solution, I know.

this.Invoke((MethodInvoker)delegate
{
    refreshTimer.Enabled = true;
    refreshTimer.Start();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM