473,396 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Thread Abort

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
5 3111
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Daisy | last post by:
From this page: http://www.c-sharpcorner.com/2/mt_beginner1.asp Thread class's Abort method is called to kill a thread permanently. Make sure you call IsAlive before Abort. if (...
7
by: Morris | last post by:
I want to abort a running thread, so I call MyThread.abort() function. My problem is this thread runs "almost" like a while(true) loop and I don't want the Abort() function interrupts the thread at...
20
by: Doug Thews | last post by:
I ran into an interesting re-pain delay after calling the Abort() method on a thread, but it only happens the very first time I call it. Every time afterward, there is no delay. I've got a...
18
by: Urs Vogel | last post by:
Hi I wrote an application server (a remoting sinlgeton), where processes must be stopped in very rare cases, done thru a Thread.Abort(). Occasionally, and only after a Thread.Abort(), this...
1
by: benmorganpowell | last post by:
I have a small windows service which connects to a POP3 server at defined intervals, scans the available messages, extracts the required information and inserts the data into a SQL database. I am...
2
by: Mark Denardo | last post by:
I'm trying to abort a suspended thread, but I get a ThreadStateException: An unhandled exception of type 'System.Threading.ThreadStateException' occurred in mscorlib.dll Additional...
6
by: Joe HM | last post by:
Hello - I have a function that calls Thread.Abort() to stop a thread in a _Closed() Method of a GUI. The thread contains a blocking call on a TCP socket and that is the easiest way to stop...
23
by: Boltar | last post by:
Hi I'm writing a threading class using posix threads on unix with each thread being run by an object instance. One thing I'm not sure about is , if I do the following: myclass::~myclass() {...
5
by: andrew | last post by:
Hi, I have the following issue with the Thread.Abort(): The main thread creates a worker thread which waits on a process termination. void ThreadProc() { Process proc =...
6
by: mehdi | last post by:
Hi folks, You know, the Thread class has got a method named Abort which according to the msdn: "Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.