Safest location for a Thread.Abort()

  • Thread starter Thread starter Wayne M J
  • Start date Start date
W

Wayne M J

I have been using threads on and off, but I am always coming across a
situation that baffles me: where is the best place to put a Thread.Abort()
call?

I have a form, when the button event is fired, a thread is created, and it
will continue on its merry way until I force the thread to `die` via the
IDE.

I have in the past used Form_Close and Form_Closing as 2 locations for the
Thread.Abort() call, but in recent times, I have found that it does not
always work, but for the most part it works fine.
 
Hi Wayne,

The better approach would be to use some other mechanism to end the thread
(event perhaps - signal it when you want the thread to end or something like
that) as Abort has some "features".
 
Wayne M J said:
I have been using threads on and off, but I am always coming across a
situation that baffles me: where is the best place to put a Thread.Abort()
call?

I have a form, when the button event is fired, a thread is created, and it
will continue on its merry way until I force the thread to `die` via the
IDE.

I have in the past used Form_Close and Form_Closing as 2 locations for the
Thread.Abort() call, but in recent times, I have found that it does not
always work, but for the most part it works fine.

I would personally avoid using Thread.Abort at all. Either use
background threads if you want the threads to die when the rest of the
app closes, or signal that you want the other threads to stop in a
timely manner.

See
http://www.pobox.com/~skeet/csharp/multithreading.html#worker.threads
for an example.
 
That threading-tutorial got quite cool!

If you'd put some more details in it, and published it as a book, I'd buy
it.

Niki
 
Niki Estner said:
That threading-tutorial got quite cool!

If you'd put some more details in it, and published it as a book, I'd
buy it.

LOL - it's nearly as detailed as I know. I'd have to be treading in the
uneasy territory of becoming an expert to put much more into it, at
least in terms of technical details - I could write examples for ages,
of course :)

I'm just hoping I get time to actually finish it some time. I have a
sneaking suspicion that by the time I get round to writing up timers,
I'll have thought of something else which should be in there...
 
Back
Top