|
-
December 6th, 2005, 05:21 AM
#1
Stop thread
Hi everyone. I have a MFC application written in Visual Studio 6.0. I am using a thread calling the AfxBeginThread function:
Code:
m_pThread = AfxBeginThread(ThreadFunc,phObjectHandle,THREAD_PRIORITY_NORMAL,0,0,NULL);
What I want is to stop the thread if some button is pressed. The function called in ThreadFunc() is extracted from a dll and thus I cannot stop the thread from inside the function. Any ideas how to do this?
Thanx in advance.
Regards,
Theodore
-
December 6th, 2005, 05:25 AM
#2
-
December 6th, 2005, 05:55 AM
#3
Re: Stop thread
Thanx for the reply. I am not sure I got it right:
To provide a clean exit of the thread an event can be set which was passed as a thread parameter and which is frequently checked by the thread procedure.
As said above, my thread function only calls a function extracted from a dll. So how could I check the parameter frequently in the thread procedure?
-
December 6th, 2005, 06:14 AM
#4
-
December 6th, 2005, 06:14 AM
#5
Re: Stop thread
 Originally Posted by yiannakop
Thanx for the reply. I am not sure I got it right:
As said above, my thread function only calls a function extracted from a dll. So how could I check the parameter frequently in the thread procedure?
u mean like this ?
Code:
UINT ThreadFunc(LPVOID l)
{
function_call_to_dll();
}
-
December 6th, 2005, 06:19 AM
#6
-
December 7th, 2005, 12:01 AM
#7
Re: Stop thread
u can only use terminatethread in this case , and not recommened , otherwise that code shld be in that dll..
-
December 9th, 2005, 12:34 PM
#8
Re: Stop thread
Thanx alot, I'll try that.
-
December 9th, 2005, 02:12 PM
#9
Re: Stop thread
Rather than terminating the thread, another approach would be to create a exit event (shared between both threads). With this approach, the secondary thread would periodically check to see if the event was set and exit if necessary. The primary thread (i.e. the controlling thread) would set this event when appropriate. As a backup, the primary thread could still terminate the thread if it fails to respond to the exit event. In general terminate thread should only be used as a last resort.
Arjay
-
December 10th, 2005, 05:07 AM
#10
Re: Stop thread
Arjay > His method seems to be a shallow wrapper around some arbitrary function. So there doesnt seem to be much to gain by using any kind of signalling mechanism.
Yiannakop> When you use TeminateThread,
1. The thread's initial stack is not deallocated.
2. Loaded dll's are not notified of the thread's demise.
3. Heap locks are not released.
4. Entered critical sections are not released.
5. Kernel32 can be left in a messed up state for the rest of your process.
You should look up the MSDN entry for TerminateThread before you decide to use TerminateThread, as the consequences are drastic!
In your situation, the only viable alternative is to make it a non-cancellable operation.
Visit my blog on http://360.yahoo.com/raghupathys
------------------------------------------------------------------------------------------
Do what you feel in your heart to be right, for you'll be criticized anyway.
You'll be damned if you do and damned if you don't.
- Eleanor Roosevelt
-
December 10th, 2005, 04:24 PM
#11
Re: Stop thread
 Originally Posted by raghupathys
So there doesnt seem to be much to gain by using any kind of signalling mechanism.
In my opinion, signalling a thread to exit is always preferable to termination (your quote from MSDN supports this position).
Even if the function code contains in the dll doesn't contain any resources that need to be cleaned up 'today', in the future it may so it's better to provide a mechanism to exit cleanly and always worth the slight amount of extra work.
Arjay
-
December 10th, 2005, 08:28 PM
#12
Re: Stop thread
Arjay> You are right, signalling the thread to return is the best way to end a thread. But in this situation, his thread proc consists of just one method call to some arbitrary code
Code:
UINT ThreadFunc(LPVOID l)
{
function_call_to_dll();
}
So there's no point adding any kind of listening mechanism into "ThreadFunc". Any kind of listening mechanism would need to be done in "function_call_to_dll". Which I believe is not possible, as it is an exported function from a 3rd party dll.
So the only viable solution would seem to be, to make it a non-cancellable operation. That is - once you create the thread, let it complete.
Visit my blog on http://360.yahoo.com/raghupathys
------------------------------------------------------------------------------------------
Do what you feel in your heart to be right, for you'll be criticized anyway.
You'll be damned if you do and damned if you don't.
- Eleanor Roosevelt
-
December 10th, 2005, 10:07 PM
#13
Re: Stop thread
While it's true that the thread func is not in a position to exit at the whim and fancy of the main thread... The correct thing to do would be to make the main thread wait for the Thread Function to finish processing - using WaitForSingleObject.
Like this -
Code:
// Inside main thread
WaitForSingleObject (m_hThread, INFINITE);
Last edited by Siddhartha; December 10th, 2005 at 10:09 PM.
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
|