By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,349 Members | 1,307 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,349 IT Pros & Developers. It's quick & easy.

Thread Abort

P: n/a
Why I can't abot a susspended thread.
Who can terminat a thread imediatly without consider to its stat or execution?
Nov 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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?

Nov 17 '05 #2

P: n/a
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?

Nov 17 '05 #3

P: n/a
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]" <Yo**@discussions.microsoft.com> wrote in message
news:CA**********************************@microsof t.com...
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?

Nov 17 '05 #4

P: n/a
Actualy I know about this API, But I don't know can I call it from my C#
application,How HANDLE define in C# ? I didn't find any example in C# doning
that,I will be so glade if you show me the way .

BOOL TerminateThread(
HANDLE hThread,
DWORD dwExitCode
);

Belive me I have a very good reasone to do that, since I work with hardware
and I can't wait until Abort finish his job, Abort function takes long time
to finish , spicialy in the scenarios I minsioned before , which is my
problem .
"Willy Denoyette [MVP]" wrote:
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]" <Yo**@discussions.microsoft.com> wrote in message
news:CA**********************************@microsof t.com...
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?


Nov 17 '05 #5

P: n/a
You can obtain the handle to the current thread by calling Win32 API
GetCurrentThread().
The reason all these things are hidden by .NET is just because they
shouldn't be used.
You won't find any sample in C# doing what you are trying to achieve,
believe me you better kill your process instead of trying to kill a thread.
Willy.
"[Yosi]" <Yo**@discussions.microsoft.com> wrote in message
news:EC**********************************@microsof t.com...
Actualy I know about this API, But I don't know can I call it from my C#
application,How HANDLE define in C# ? I didn't find any example in C#
doning
that,I will be so glade if you show me the way .

BOOL TerminateThread(
HANDLE hThread,
DWORD dwExitCode
);

Belive me I have a very good reasone to do that, since I work with
hardware
and I can't wait until Abort finish his job, Abort function takes long
time
to finish , spicialy in the scenarios I minsioned before , which is my
problem .
"Willy Denoyette [MVP]" wrote:
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]" <Yo**@discussions.microsoft.com> wrote in message
news:CA**********************************@microsof t.com...
>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?


Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.