Thread Abort

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Why I can't abot a susspended thread.
Who can terminat a thread imediatly without consider to its stat or execution?
 
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
 
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
 
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.
 
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 .
 
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.
 
Back
Top