tags:

views:

69

answers:

7

hi

First, sorry for my bad english writing.

Suppose that we have a win form (C#) that has a timer. The timer interval has been set to 60000 milsec. So its tick event will be fired every 1 milute. Suppose we have written a method that handles tick event called Timer1_Tick. What if the job needs more that 1 minute to complete?

A: 

Store the current state of the process in a field or property and start the process only if the state is no "running".

Pierre 303
A: 

Another timer event will likely be queued, causing Timer1_Tick to be called again almost immediately after it returns. (IIRC, though, timer ticks are one of the lowest priority messages, so it'll probably handle any other messages it's had queued up to that point first, except maybe paint messages).

Note, if your function takes longer than 2 minutes to run, it's possible (read: likely) that only the latest tick will be in the queue.

If your tick processing takes longer than the timer interval, you should look into raising the interval. Either way, you should probably be doing the work in a background thread and making sure you don't start another thread if the last tick's task isn't done. Otherwise you could end up with hordes of threads all slowing each other down til your program collapses under its own weight.

cHao
+1  A: 

So set a flag when you start the job and check the flag when the timer fires. If the flag is set, do nothing in the timer handler. Remember to clear the flag when the job completes.

Are you spinning off a worker thread to do the job?

Bob Moore
+1  A: 

Setup a flag that will allow you to check if the long running job has finished and only run the job if it has finished. Don't forget to reset the flag after finishing the long running job:

// field
private bool finishedWork = true;

public void Timer1_Tick(Object o, EventArgs e)
{
   if (finishedWork)
   {
     finishedWork = false;

     // do work

     finishedWork = true;
   }
}

Another option is to simply disable the timer between operations:

public void Timer1_Tick(Object o, EventArgs e)
{
   if (finishedWork)
   {
     Timer1.Enabled = false;

     // do work

     Timer1.Enabled= true;
   }
}
Oded
A: 

Disable the timer at the start of Timer1_Tick and then enable it again afterwards?

SLC
+1  A: 

You've got several options, here's four I can think of:

  1. Abandon the current job to start the new one. The big downside of this one is, of course, if the current job can't be stopped.
  2. Wait for the current job to finish before starting the new one. This might leave you with a queue of pending jobs if each one takes more than a minute.
  3. Don't start the new job. Let the current one finish and then wait for the next timer interval to start the new job.
  4. Increase the interval between jobs. This is just putting off the problem.

There is no right answer. You'll have to work out what's best and works for your situation.

I'd go for #3 as my first solution.

ChrisF
A: 

There are multiple types of Timers in .Net: One is in a System.Timers namespace, another is in System.Windows.Forms namespace and another in System.Threading.

The System.Windows.Forms.Timer control is based on UI thread and message loops, meaning it will queue the timer events and if your handler exceeds the interval, it will be called immediately after ending.

The other two timers are based on threading, and are very accurate. They will reenter you handler after the time elapsed.

František Žiačik