Re: How to interrupt a method ?
- From: "Manfred" <ml@xxxxxxxxxxxxxxxxxx>
- Date: 13 Jan 2006 10:33:12 -0800
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
.
- Follow-Ups:
- Re: How to interrupt a method ?
- From: Willy Denoyette [MVP]
- Re: How to interrupt a method ?
- References:
- How to interrupt a method ?
- From: Const
- Re: How to interrupt a method ?
- From: Nicholas Paldino [.NET/C# MVP]
- Re: How to interrupt a method ?
- From: Naveen
- Re: How to interrupt a method ?
- From: Willy Denoyette [MVP]
- How to interrupt a method ?
- Prev by Date: Re: ASCII charcters from 0x21 to 0xff
- Next by Date: Memory management question
- Previous by thread: Re: How to interrupt a method ?
- Next by thread: Re: How to interrupt a method ?
- Index(es):
Relevant Pages
|
Loading