Click to See Complete Forum and Search --> : Kill a thread? before it kills the app! HOWTO
Muthu Ram
October 6th, 1999, 11:49 AM
How to kill a worker thread, or stop the thread manually using api calls? the main thread creates another worker thread to do some polling.. if the main thread decides to kill the worker thread, before the worker thread can die how to do it?
how do you do concurrent processing with COM anyways?
I am doing something like this..
'start the thread here
sub startAThread ()
startTheWork
createThread ' the thread is running doing work
contineTheWork ' the main thread is busy
finishedWork
'now if the thread had died, no probs
'but if it is still working, no good, killit.
killThread 'at this point kill the thread anyways
'the entire app crashes unfortunately
end sub
Lothar Haensler
October 7th, 1999, 02:11 AM
you can "kill" a thread by means of the TerminateThread api.
This is "bad design" IMHO, because there won't be any cleanup of thread resources.
IMHO you should rather raise an event (SetEvent API) and cause the thread to exit its processing.
Muthu Ram
October 7th, 1999, 01:18 PM
I am using the TerminateThread API call but it kills my app. It gives me some memory reference problem, and i am investigating it currently.
Ravi Kiran
October 9th, 1999, 03:19 AM
You are saying "Worker thread" meaning no GUI stuff. What is meant by polling? Doing same function repetitively?
My Idea:
Just have a simple global variable, which when set to true will stop the thread. ie the thread function will look at this status variable every once in a while. If you are doing a long processing on worker thread, either in a loop or stepbystep execution, sure there will be some points where you can check this flag.
So your logic goes like this:
sub startAThread ()
startTheWork
createThread ' the thread is running doing work
contineTheWork ' the main thread is busy
finishedWork
'now if the thread had died, no probs
'but if it is still working, no good, kill it.
'killThread 'at this point kill the thread anyways
'the entire app crashes unfortunately
AskitToTerminateItself
end sub
' You Thread Fn Logic
Function WorkerThread()
if IsTimeToQuit() then exit function
DoStep1ofLongProcessing
if IsTimeToQuit() then exit function
DoStep2ofLongProcessing
...
' Or if it is a loop processing:
for i = 0 to SomeRealBigNumber
if (i mod SomeDecentNumber) = 0 then
' check for stoping
if IsTimeToQuit() then
Exit for
end if
end if
' Otherwise
DoWhatEverStuff
next i
end sub
sub AskitToTerminateItself()
m_bStopNow = true
end if
function IsTimeToQuit() as boolean
IsTimeToQuit= m_bStopNow
end function
Or if you want real good stuff, take a look thread synchronisation topics on MSDN.
You can design your thread function like this also:- Every once in a while, try accessing a synchronisation object. IF it fails to achieve the access for a previously set no. of times then it is time to quit!, return from thread function with approp exit code. So the master thread will keep the access of the sync object with it w/o relinquishing for some time. This will automatically force the other thread to terminate.
So the master thread will keep holding the object and keeps looking at the status of the thread till it quits!!
RK
Muthu Ram
October 12th, 1999, 01:33 PM
i am using the same technique maintaining the thread state and timing out. but since i made the class as an activex dll to do marshalling i am stuck up in a big mess. i cannot figure out whats happening when.. it ends up in app crash anyways.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.