Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Wanted to know if thread timer object continues to fire even if the task being performed it's done.

Example:
C#
System.Threading.Timer t = new System.Threading.Timer(MyMethod,null,0,1000);

void MyMethod(object o)
{
  // task that completes at varying times but NEVER less than the timer's interval
}
Posted
Updated 18-Aug-11 15:54pm
v3

Yup. The timer fires a TICK event, then restarts immediately. It does not wait for the task to complete, it just fires an event.

If you want it to stop, then use the stop routine.
 
Share this answer
 
v2
In case you to run your next task only after finishing the current task is over, try to set a global flag and within timer do new the task based on the status of flag.
 
Share this answer
 
v2
The simple fact that a timer keeps firing even when the tick handler is being processed makes the code using timers vulnerable. It's much better to use threads whenever possible avoiding timers.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900