As we know each timer elapsed method callbacks on a separate thread. Is there a way to abort the timer thread in c#? Suppose a timer thread is blocked in a long running operation. How would a new thread abort the blocked thread? the Thread.CurrentThread points to the calling thread instead of the blocked thread.
-
Not all timer callbacks do come on in on separate threads. Which timer are you using? – Brian Gideon Apr 27 '10 at 13:11
-
What do you mean by "abort the timer thread"? Do you mean to stop the timer from ticking again? or ... what? – Lasse V. Karlsen Apr 27 '10 at 13:13
3 Answers
Implement your timer as a BackgroundWorker with WorkerSupportsCancellation and check for CancelPending inside your manually written timer?

- 4,437
- 2
- 29
- 28
-
I believe that even if u cancel the background thread worker. it continues processing the working in the background. – TrustyCoder Apr 28 '10 at 04:16
-
It can continue processing. It is up to you to check for CancellationPending in your BackgroundWorker's DoWork method and stop if this is true. – Daniel Rose Apr 28 '10 at 08:28
-
Uh. yes. I didn't realize I had to explicitly say that. BackgroundWorker is essentially a Thread object with two benefits: - it lets you request cancellation (externally) and then check if a cancellation was requested (internally) so you can have a clean exit - capture the thread termination event – Oren Mazor Apr 28 '10 at 13:29
Seems to me the simplest way is to store the thread object before the timer thread does any work with code like this:
timerThread = System.Threading.Thread.CurrentThread;
Then when you need to abort it, call
timerThread.Abort();
Edit: Per comments, I will say that aborting threads is not a good idea. It's better to have them terminate gracefully. I would suggest sending some sort of message to the thread, possibly by simply setting a flag, and having the thread terminate itself when it finds that message.

- 25,079
- 9
- 80
- 146
-
That's really not a good idea. It will raise a `ThreadAbortException` inside the thread which may or may not be prepared to handle it. Even worse if the thread ends up being a `ThreadPool` thread. – Aaronaught Apr 27 '10 at 13:09
-
Right, aborting threads is not a good idea, but that was the question. – BlueMonkMN Apr 27 '10 at 13:18
-
System.Threading timers uses the ThreadPool and the ThreadPool re-use threads killing one under it's feet is never a good idea. – Julien Roncaglia Apr 27 '10 at 16:48
-
suppose a thread is blocked in a long running operation. How would a new thread abort the blocked thread? – TrustyCoder Apr 28 '10 at 08:07
-
Thread.Abort is the "rudest" way I know to interrupt a thread, so if Abort doesn't stop a blocked thread, I don't know what will. – BlueMonkMN Apr 28 '10 at 11:14
Aborting threads results in asynchronous exception. So the best way to abort threads is not to abort at all. Instead use mutex and semaphores to signal cancellation and then continuously poll for the cancellation state. if cancel is signaled do nothing.

- 4,749
- 10
- 66
- 119