Re: Thread Abort
- From: "Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx>
- Date: Fri, 29 Jul 2005 17:51:55 +0200
The only way to abruptly terminate a thread is by calling Win32's API
TerminateThread(), BUT there is no valid reason to call it from managed code
and you should have very good reasons to do this from unmanaged code.
By doing this you simply corrupt your process, it might continue to work for
a small amount of time but sooner or later it will fail (crash or behave
unexpectedly). The same goes for Thread.Abort on another thread than the
calling thread, but here at least you have a chance to unload the
Application domain (if this runs in a non default AppDomain).
Willy.
"[Yosi]" <Yosi@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:CA7CCE1A-BA26-425D-B9A0-6C40D2A5D78B@xxxxxxxxxxxxxxxx
>H Marki,
> You are right about such a Boolean inside the thread this what I done.
> But , in some cases this not good enough, one of the scenarios is as
> following :
>
> I create a thread , the thread call DLL function , this function write to
> an
> IO address or write buffer to external device using LPC/USB (takes 2
> minutes)
> , while this function write to IO, another thread which call DLL function
> that polling (read) GPIO (General Port IO) , read in loop until this GPIO
> become zero this happen for example when power failed occur, in this case
> I
> want to abort the write thread immediately and not to wait until it is
> finish
> . how can I do that ? the Boolean solution is not good enough . I want
> function like process.Kill immediately . There are a lot of reason in our
> industry that need such terminator function
>
> "Mark R. Dawson" wrote:
>
>> Hi Yosi,
>> there should be no reason why you cannot abort a suspended thread, what
>> behaviour are you seeing that makes you think the thread is not being
>> aborted?
>>
>> Even calling Abort on a thread does not guarantee that the thread will
>> stop immediately. Aborting a thread throws a ThreadAbortException which
>> will
>> cause catch and finally statements to be entered, plus the
>> ThreadAbortException is rethrown at the end of each catch automatically
>> so
>> you may go into many regions of code befoer the thread is officially
>> aborted.
>>
>> You should really try to find some graceful way of stopping your thread,
>> such as setting a boolean inside the thread method to indicate it should
>> stop, trying to stop the thread at arbitary locations in your code may
>> cause
>> unforseen errors.
>>
>> Mark
>>
>> "[Yosi]" wrote:
>>
>> > Why I can't abot a susspended thread.
>> > Who can terminat a thread imediatly without consider to its stat or
>> > execution?
.
- Follow-Ups:
- Re: Thread Abort
- From: [Yosi]
- Re: Thread Abort
- References:
- Thread Abort
- From: [Yosi]
- RE: Thread Abort
- From: Mark R. Dawson
- RE: Thread Abort
- From: [Yosi]
- Thread Abort
- Prev by Date: Re: Event handler not being created
- Next by Date: Data Oriented vs Object Oriented Design
- Previous by thread: RE: Thread Abort
- Next by thread: Re: Thread Abort
- Index(es):
Relevant Pages
|