Re: How to interrupt a method ?



Other wrote:
"This is a bad suggestion, you should never call Thread.Abort from user
code."
"Calling Thread.Abort is a very BAD BAD BAD BAD (did I say BAD) idea!"

I disagree with these statements. Calling Thread.Abort() is perfectly
acceptable. Let me explain my point of view.

An application with one thread handling the UI and a different thread,
the worker thread, executing some lengthy operation is a perfectly
acceptable solution. That way the UI stays responsive. There are
obviously other cases where starting and terminating threads is the
best (and sometimes the only) design choice, e.g. how do you cancel a
blocking call? Terminating the process?

Polling a flag is certainly not an option. It is not deterministic in
particular if the code in the thread does not poll the flag, e.g. when
a blocking call is made. (How do you time out when you try to get
exclusive access to a file?)

There is, however, a suggested coding technique for properly shutting
down a thread when it is being terminated using Thread.Abort(). The
runtime throws a ThreadAbortException which should be handled in the
thread. See documentation at
http://msdn2.microsoft.com/en-us/library/cyayh29d.aspx.

Best regards,
Manfred.
---
Manfred Lange
http://www.manfred-lange.com
http://manfredlange.blogspot.com
ml at agileutilities dot com

.



Relevant Pages

  • Re: How to interrupt a method ?
    ... | "This is a bad suggestion, you should never call Thread.Abort from user ... Calling Thread.Abortis perfectly ... Terminating the process? ... that are transitioning into unmanaged code. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Why all the NON-AMP POST
    ... ranting or even a bit of name calling. ... someone and making threats, or spewing revolting fantasies involving sexual ... violence against children with the suggestion someone you dislike is the ...
    (alt.guitar.amps)
  • Re: Windows Explorer has encountered - PCBUTTS Impersonating me again
    ... NNTP-Posting-Host: 76.198.93.184 ... The below suggestion provided by PaulM. ... Following any advice from PCBUTTS would put your system at risk. ... Calling an illegal alien an "undocumented worker" is like calling a ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: Time Warner cuts newsgroups in KC
    ... After calling them and getting ... the run around and the suggestion from Time Warner that I use a ... premium news reader service I'll use Google until I get over my mad. ...
    (rec.games.pinball)
  • Re: Time Warner cuts newsgroups in KC
    ... After calling them and getting ... the run around and the suggestion from Time Warner that I use a ... premium news reader service I'll use Google until I get over my mad. ...
    (rec.games.pinball)

Loading