|
-
April 22nd, 2008, 04:49 AM
#1
stop a thread
Hello everyone,
About using Abort or Interrrupt to stop a thread, I have two questions.
1. What are the pros and cons about Abort v.s. Interrupt?
2. If I program a background thread, compared with normal thread, will there be any special points to take care of (e.g. code in different) if the thread is stopped with Abort (or Interrupt)?
thanks in advance,
George
-
April 22nd, 2008, 01:16 PM
#2
Re: stop a thread
Don't use Abort, signal the thread to exit instead. Research the details in msdn.
-
April 22nd, 2008, 09:27 PM
#3
Re: stop a thread
Thanks Arjay,
This is the only issue I found from MSDN about Thread.Abort. Are there any other issues?
http://msdn2.microsoft.com/en-us/lib...ta(VS.80).aspx
--------------------
There is also a chance that a static constructor could be aborted. In rare cases, this might prevent instances of that class from being created in that application domain.
--------------------
 Originally Posted by Arjay
Don't use Abort, signal the thread to exit instead. Research the details in msdn.
regards,
George
-
April 22nd, 2008, 09:36 PM
#4
Re: stop a thread
Abort is the .Net equivalent of the Win32 api's TerminateThread or TerminateProcess. In general threads and processes should be signalled to exit rather than terminated; otherwise unpredictable things can happen, resources not released, and so on. Read up on these api's in msdn for more info.
-
April 22nd, 2008, 09:47 PM
#5
Re: stop a thread
Thanks Arjay,
You are so knowledgable. But this time I am not caring too much about implementation details about .Net API. :-)
I am wondering,
1. Why Thread.Abort will cause resource leak (e.g. expected finally block or Finalizer is not executed)?
2. How do we avoid such leak?
 Originally Posted by Arjay
Abort is the .Net equivalent of the Win32 api's TerminateThread or TerminateProcess. In general threads and processes should be signalled to exit rather than terminated; otherwise unpredictable things can happen, resources not released, and so on. Read up on these api's in msdn for more info.
regards,
George
-
April 22nd, 2008, 10:05 PM
#6
Re: stop a thread
George, if you can't find the answer in msdn, you need to roll up your sleeves, write some code, and test your ideas out. With a couple of Console.Write or Debug.Write statements you can find out quick enough.
As far as preventing a resource leak when using Abort, the answer is simple - don't use Abort (or TerminateThread or TerminateProcess). The Msdn documentation pretty much tells you this.
-
April 22nd, 2008, 10:22 PM
#7
Re: stop a thread
Thanks Arjay, :-)
How about Thread.Interrupt, also have some resource leak issues? :-)
In my point of view, if we handle ThreadInterruptException, release resource, then it should be fine and no resource leak.
 Originally Posted by Arjay
George, if you can't find the answer in msdn, you need to roll up your sleeves, write some code, and test your ideas out. With a couple of Console.Write or Debug.Write statements you can find out quick enough.
As far as preventing a resource leak when using Abort, the answer is simple - don't use Abort (or TerminateThread or TerminateProcess). The Msdn documentation pretty much tells you this.
regards,
George
-
April 22nd, 2008, 11:25 PM
#8
Re: stop a thread
 Originally Posted by George2
Thanks Arjay, :-)
How about Thread.Interrupt, also have some resource leak issues? :-)
In my point of view, if we handle ThreadInterruptException, release resource, then it should be fine and no resource leak.
regards,
George
See my previous post about rolling up your sleeves, writing some code, and trying it out.
Oh, and what's wrong with everyone's suggestion of just signalling the thread to exit?
-
April 22nd, 2008, 11:44 PM
#9
Re: stop a thread
Thanks Arjay,
My thread is doing some time consuming calculations, how could I receive signal in the midddle? :-)
My idea is, if I have to use signal, I need to check in the middle of calculations, and using the timeout form of WaitOne. But my concern is it will degrate the performance of my calculation (my concern is wait signal timeout). Any suggestions?
 Originally Posted by Arjay
See my previous post about rolling up your sleeves, writing some code, and trying it out.
Oh, and what's wrong with everyone's suggestion of just signalling the thread to exit?
regards,
George
-
April 23rd, 2008, 12:24 AM
#10
Re: stop a thread
 Originally Posted by George2
Any suggestions?
Yes. Write it with an event and check the event as often as necessary within the thread.
Then profile the application and MEASURE the difference in performance.
-
April 23rd, 2008, 01:47 AM
#11
Re: stop a thread
Thanks Arjay,
1.
If I set the event in signalled status, then next time, when the thread checks the singal by using WaitOne, it will be blocked, right?
2.
You mean using the timeout form of wait to check event, like this one?
http://msdn2.microsoft.com/en-us/lib...t0(VS.80).aspx
 Originally Posted by Arjay
Yes. Write it with an event and check the event as often as necessary within the thread.
Then profile the application and MEASURE the difference in performance.
regards,
George
-
April 23rd, 2008, 03:48 AM
#12
Re: stop a thread
Yes, use a timeout of zero to quickly determine if the event has been set.
-
April 23rd, 2008, 04:56 AM
#13
Re: stop a thread
Thanks Arjay,
I am writing code and I have some issues dealing with the exitContext parameter of WaitOne, do you have any ideas?
http://www.codeguru.com/forum/showthread.php?t=451479
 Originally Posted by Arjay
Yes, use a timeout of zero to quickly determine if the event has been set.
regards,
George
-
April 23rd, 2008, 12:25 PM
#14
Re: stop a thread
George, please don't cross post.
It is my only hope that one day we will respond to a post without getting asked yet one more followup question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|