The timer does not want to start again after it is turned off

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

The tick event calls a method that starts a new thread and disables the timer, and then, when the thread is running with it, it starts the timer again, but the problem is that now it does not go out after it is turned on.

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();
     }
+3
source share
7 answers

As @grzenio said , it looks like your problem is that you are making cross-thread calls in Windows Form Control, which was created on another thread.

.NET 4.5 (# 5.0), async/await keywords,

, async "DoStuff":

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

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

        _Timer.Enabled = true;
    }

:

  • async Timer_Tick.
  • await Task.Run DoStuff.

DoStuff , DoStuff , , , Tick.

+10

GUI GUI. DoEvents, 100% . System.Threading.Timer. .

+3

System.Threading.Timer, , , Change , , , .

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);
    }
}
+2

Windows Forms , , , ., , # Windows Forms - GUI ?

+1
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();
     }
+1

. , . , , .

Timer, , , . , .

. , testtimer.Tick, , .

, Enabled = true. , , , .

, , Enabled = false/true.

0

If you really want to stick to the GUI timer and run it from a thread other than the UI, you can try to make similar material and then write to the GUI from a non-interface.

Not a perfect solution, I know.

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

All Articles