Reputation: 26517
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?
Upvotes: 0
Views: 3091
Reputation: 498904
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;
}
}
Upvotes: 2
Reputation: 7612
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.
Upvotes: 0
Reputation: 86506
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.
Upvotes: 1
Reputation: 137108
You've got several options, here's four I can think of:
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.
Upvotes: 2
Reputation: 52922
Disable the timer at the start of Timer1_Tick and then enable it again afterwards?
Upvotes: 0
Reputation: 6894
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?
Upvotes: 1
Reputation:
Store the current state of the process in a field or property and start the process only if the state is no "running".
Upvotes: 0