0
votes

I got a System.Timers.Timer (Even if Threading.Timer is more powerfull, I need the restart option) which should call an Event(Function) for data exchange. My Timer Init:

private System.Timers.Timer _scheduler;     //on class level  
_scheduler = new System.Timers.Timer(double.Parse(Config.TimeIntervall));
_scheduler.AutoReset = false;
_scheduler.Elapsed += (o, args) => InterfaceSingleRun();
_scheduler.Start();

The function starts a task, and in the finally statement I got

_scheduler.Start()

to restart my Timer, which should start counting down the intervall again

My Problem: I want to prevent overlapping, because I don't know how long the Task will be running (depends on Server), but the _schedulter.Start() just restarts the Elapsed Event, instead of waiting the intervall time. Could you please tell me if I just misunderstood the MSDN-Site/the Timer got some issues and how to fix them/or my code is just messed up :)

Thanks

EDIT: Solved the Problem, thanks to Hans Passant. I was working with seconds, which the program interpreted as milliseconds.

2
My crystal ball says that Config.TimeIntervall is in seconds instead of milliseconds. - Hans Passant
I want your crystal ball.... Indeed, it is. Just can't test it right now because my code doesn't work.. You will get an answer as soon as I fixed some other little problems ;) - DaJohn
Don't bother, just multiply it by 1000. - Hans Passant
All Hail your crystal ball :) fixed my code, multiplied by 1000 and everything is fine.. was testing with - I thought - 20 sec, which were 20 ms and that looks like immediate restart. If you get me something to mark as solution, I will do. Thanks for fixing - DaJohn

2 Answers

0
votes

If i understand correct, you want to prevent a second trigger while the code inside the timer_tick is running.

i use a global bool like this:

class Program
{
    private static bool timercodeRunning;

    static void Main(string[] args)
    {
        var timer = new System.Timers.Timer(5000);

        timer.Elapsed += timer_Elapsed;
        timer.Start();
    }

    static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        if (!timercodeRunning)
        {
            timercodeRunning = true;
            try
            {
                //DO SOME STUFF


                timercodeRunning = false;
            }
            catch (Exception)
            {
                timercodeRunning = false;
                throw;
            }

        }
    }
}
0
votes

instead of bool i recommended you to try locking. here is modified code of Gelootn

    class Program
{
    private static object timercodeRunning = new object();

    static void Main(string[] args)
    {
        var timer = new System.Timers.Timer(5000);

        timer.Elapsed += timer_Elapsed;
        timer.Start();
    }

    static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        lock (timercodeRunning)
        {
            try
            {
                //DO SOME STUFF
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}