Stop all threads in Threading.Timer in Callback method?
-
As I've understand it the callback method specified in the Timer constructor can be executed multiple times in parallell. It does not wait for the callback to finish. This can cause issues for me so I need to be sure that only one callback (or the task it will perform) will run one at a time and if the task suceeds the timer must stop. How can I do that? Will the following do the job?
System.Threading.Timer timer; private void StartThreadTimer() { timer = new System.Threading.Timer(TimerCallback, "hepp", 0, 5000); } void TimerCallback(object callbackObj) { if (Monitor.TryEnter(lockerObject)) { try { Thread.Sleep(1000); // If this task succeeds the timer should stop and no callbacks must be allowed to be called again timer.Dispose(); //Can this guarantee that no more callbacks will be called? } finally { Monitor.Exit(lockerObject); } } }
If the above is not safe enough, how should I do it? Thanks! -
As I've understand it the callback method specified in the Timer constructor can be executed multiple times in parallell. It does not wait for the callback to finish. This can cause issues for me so I need to be sure that only one callback (or the task it will perform) will run one at a time and if the task suceeds the timer must stop. How can I do that? Will the following do the job?
System.Threading.Timer timer; private void StartThreadTimer() { timer = new System.Threading.Timer(TimerCallback, "hepp", 0, 5000); } void TimerCallback(object callbackObj) { if (Monitor.TryEnter(lockerObject)) { try { Thread.Sleep(1000); // If this task succeeds the timer should stop and no callbacks must be allowed to be called again timer.Dispose(); //Can this guarantee that no more callbacks will be called? } finally { Monitor.Exit(lockerObject); } } }
If the above is not safe enough, how should I do it? Thanks!System.Threading.Timer timer;
private void StartThreadTimer()
{
timer = new System.Threading.Timer(TimerCallback, "hepp", 5000, Timeout.Infinite);
}void TimerCallback(object callbackObj)
{
Thread.Sleep(1000);
if (!success) {
// restart
timer.Change(5000, Timeout.Infinite);
}
}I would use a single shot timer and restart it in the callback if the task is not successful. This way the callback can never be reentered and the lock isn't needed. Alan.
-
As I've understand it the callback method specified in the Timer constructor can be executed multiple times in parallell. It does not wait for the callback to finish. This can cause issues for me so I need to be sure that only one callback (or the task it will perform) will run one at a time and if the task suceeds the timer must stop. How can I do that? Will the following do the job?
System.Threading.Timer timer; private void StartThreadTimer() { timer = new System.Threading.Timer(TimerCallback, "hepp", 0, 5000); } void TimerCallback(object callbackObj) { if (Monitor.TryEnter(lockerObject)) { try { Thread.Sleep(1000); // If this task succeeds the timer should stop and no callbacks must be allowed to be called again timer.Dispose(); //Can this guarantee that no more callbacks will be called? } finally { Monitor.Exit(lockerObject); } } }
If the above is not safe enough, how should I do it? Thanks!Be a man, and use threads instead. Timers are for people that are closet VB programmers.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997 -
Be a man, and use threads instead. Timers are for people that are closet VB programmers.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997 -
I'm just curious: How would you achieve what I'm after with a separate thread? I like the idea but they're scary :-) Would I just use Thread.Sleep in that thread instead of a timer? Thanks!
Using threads requires more code, but they're a lot more flexible than timers. That, and the fact that Toimer events in Windows are the lowest priority events, and on a busy system, they're not guaranteed to be fired. That means there's a certain lack of reliability, and that's a "Bad Thing" (TM). Before committing to using threads in a production app, you should create some test apps and get used to the idea of them. There are a lot of good articles reagarding the use of multithreading here on CodeProject.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997