3

I want to raise a function periodically . When I finish one function cycle to wait some period of time and only them to start the second run.

I thought to make it like :

        timer = new System.Timers.Timer();
        timer.Interval = 1000;
        timer.Enabled = true;
        timer.Start();
        timer.Elapsed += TimerTick;


private void TimerTick(object sender, EventArgs e)
{
//My functionality
}

But seems that TimerTick is raised every secound and not secound from my last TimerTick run .

How i can solve this one ?

2
  • The problem is in asynchronous nature of the timer: if you method executes during 10 seconds, you'll get it called every second, though.
    – Iarek
    Sep 22, 2011 at 13:44
  • Try making check if you method is running now, and exit the method, so you'll execute it more synchronously.
    – Iarek
    Sep 22, 2011 at 13:45

7 Answers 7

3

You can use threads:

var thread = new Thread(o => {
    while(true)
    {
        DoTick();
        Thread.Sleep(1000);
    }
});
3
  • 1
    Thread.Sleep will block your thread, I wouldn't recommend that. Code after Thread.Sleep will not run until the 1000ms are passed and your app will appear blocked.
    – Abel
    Sep 22, 2011 at 13:52
  • Warning: as Timer suppresses all exceptions in Elapsed event, this code's behavior is potentially different from Timer, because exception in DoTick() will kill the calling thread.
    – Iarek
    Sep 22, 2011 at 13:53
  • @Abel ... and for that purpose the loop is enclosed into a separate thread.
    – Iarek
    Sep 22, 2011 at 13:54
0

You can stop your timer before doing your processing and start it again after it's done:

private void TimerTick(object sender, EventArgs e)
{
    timer.Stop();

//My functionality

    timer.Start();
}

it's also a good idea to put your functionality in a try-catch and call Start() in the finally section. (if this suits you)

0

Try the following:

private void TimerTick(object sender, EventArgs e)
{
    // get the timer that raised this event (you can have multiple 
    // timers, or the timer obj is out of scope here, so use this):
    Timer timer = (Timer) sender;

    // disable (or use timer.Stop())
    timer.Enabled = false;

    // ...
    // your code
    // ...    

    // at end, re-enable
    timer.Enabled = true;
}

you will find that the timer will now run 1000ms after your code finished. You can also use timer.Stop() and timer.Start().

0

you could always do something like this:

while (true)
{
    // your functions
    Thread.Sleep(1000)
}

you'd have to find a way to stop this through an external mechanism, but it should work.

0

You're right: the timer will run every second: it won't care what you're doing in the TimerTick.

What you can do is to stop the timer on entering the TimerTick methode.

private void TimerTick(object sender, EventArgs e)
{
  timer.Stop();   
  //My functionality
  timer.Start();
}
0

Try this:

DateTime lastDT = DateTime.MinValue;
private void TimerTick(object sender, EventArgs e)
{
    DateTime now = DateTime.Now;
    if (now - last).TotalSeconds > what_you_want
    {
        //My functionality
    }
    last = now;
}

Using this, your form (main thread) is not locked and you/user can do what you please.

0

Try

private void TimerTick(object sender, EventArgs e)
{
    timer.Stop();
    // My Functionality
    timer.Stop();
}

However, you may have an extra even fired. As per MSDN Docs:

The signal to raise the Elapsed event is always queued for execution on a ThreadPool thread, so the event-handling method might run on one thread at the same time that a call to the Stop method runs on another thread. This might result in the Elapsed event being raised after the Stop method is called. The code example in the next section shows one way to work around this race condition.

You might want to consider Thread.Sleep() instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.