473,854 Members | 1,837 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3142
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 ThreadAbortExce ption which will
cause catch and finally statements to be entered, plus the
ThreadAbortExce ption 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 ThreadAbortExce ption which will
cause catch and finally statements to be entered, plus the
ThreadAbortExce ption 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**@discussio ns.microsoft.co m> wrote in message
news:CA******** *************** ***********@mic rosoft.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 ThreadAbortExce ption which
will
cause catch and finally statements to be entered, plus the
ThreadAbortExce ption 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**@discussio ns.microsoft.co m> wrote in message
news:CA******** *************** ***********@mic rosoft.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 ThreadAbortExce ption which
will
cause catch and finally statements to be entered, plus the
ThreadAbortExce ption 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
GetCurrentThrea d().
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**@discussio ns.microsoft.co m> wrote in message
news:EC******** *************** ***********@mic rosoft.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**@discussio ns.microsoft.co m> wrote in message
news:CA******** *************** ***********@mic rosoft.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 ThreadAbortExce ption
>> which
>> will
>> cause catch and finally statements to be entered, plus the
>> ThreadAbortExce ption 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
5368
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 ( thread.IsAlive ) { thread.Abort(); }
7
3298
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 any point in the thread. In fact, I have a section of code needs to be "protected" from being interrupted. How can I make sure Abort() will not land anywhere winthin this block? In other words, the Abort() must wait until this block of code is done...
20
3041
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 delegate inside the UI that I call to update the progress meter. I use the Suspend() and Abort() methods based on button events. I can watch the progress meter increase just fine when the thread is running. When I select Start, I enable the Cancel...
18
5867
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 component becomes instabile, throwing a Windows like error (access violation on 0x00000002), not an framework exception. The component and all of its subcomponents are 100% managed code. What could go wrong with Thread.Abort()? Thanks for any hints.
1
4476
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 assuming that this is not an uncommon piece of software. I want to get an architecture that conforms as closely as possible with the recommendations from Microsoft on developing Windows Services, but to be honest I have found difficultly in...
2
4294
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 information: Thread is suspended; attempting to abort. I have a number or threads in my program - some running, some suspended. I hope I don't have to start the thread up again just to abort it??
6
5491
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 that. This thread is also outputting strings in a RichTextBox and in some rare instances I get a System.NullReferenceException when I exit the GUI. It seems like the _Closed() Method calls Thread.Abort() and then continues closing down/disposing...
23
5731
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() { : : do stuff
5
5084
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 = proc.Start("notepad.exe");
6
2946
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 terminating the thread. Calling this method usually terminates the thread." I've had a long discussion with someone on not to use the mentioned method unless under the most extreme cases. I believe that it's
0
9750
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11024
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10362
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9510
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7909
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5738
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5937
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4149
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.