Click to See Complete Forum and Search --> : how to close my program safely???????????
malli_santhi
May 22nd, 2006, 12:15 AM
My project is an MFC dialog based application. It uses CMSFlexGrid OLE control for displaying stock information in dialog box. My project uses 9 threads totally. Out of them one thread is to update that grid for every 10 sec with the data which is present in arrays. Those arrays will be filled up with some other thread which processes the message and fills those arrays. If I close the dialog box when that thread is updating that grid, a function will be called which comes under main thread which cleans up all the data of the application If I stop main thread until that GUI thread gets closed, application is going into stopping mode. If I don’t stop main thread, assertion is coming as thread wants to access GUI whose object is destroyed by that time. When I wanted to terminate thread using TerminateThread() call it worked fine, but it is too dangerous to use.
Note: This GUI thread is not creating any variables on heap.
What is the solution for this deadlock situation? Need I implement user interface thread for this instead of worker thread? Please suggest me good solution to end my application safely while GUI is getting updated by that thread.
wildfrog
May 22nd, 2006, 12:30 AM
Please suggest me good solution to end my application safely while GUI is getting updated by that thread.The normal/safe solution is to stop the threads, then exit your application. To do so you can use a boolean that the thread checks from time to time. When you want the thread to stop simply set the boolean to true:
Your thread 'loop':
// loop until we're told to stop
while (!bStop)
{
// do thread stuff
}
Then to stop the thread:
// make thread stop
bStop = true;
Then you can use WaitForSignleObject (of one of the other wait methods) to wait until the thread has stopped:
WaitForSingleObject(hThread, INFINITE);
- petter
Naumaan
May 22nd, 2006, 12:35 AM
U have to use Syncronization between threads and use event when ur application is going to close. CreateEvent, PulseEvent, SetEvent, WaitForSingleObject, these are the APIs which u can use to solve such problem . Check MSDN for further Information regarding these APIs.
wildfrog
May 22nd, 2006, 12:44 AM
U have to user Syncronization between threads and use event when ur application is going to close. CreateEvent, PulseEvent, SetEvent, WaitForSingleObject, these are the APIs which u can use to solve such problem.Thats right, exept for PulseEvent which is unreliable.
MSDN on PulseEvent:
Note This function is unreliable and should not be used. It exists mainly for backward compatibility.
- petter
malli_santhi
May 22nd, 2006, 12:52 AM
You are right. I have used events to close the thread. When dialog box gets closed one event will be set through which thread can return from controlling function. But if this GUI thread is in the function which updates GUI, and if I try to close the application, thread is not returning from that function. So event will be of no use for it. In that way thread is getting blocked there. I think you have understood the problem.
sirikrishnap
May 22nd, 2006, 05:47 AM
In dialog close event, you can set a boolean variable or an event and sleep for maximum time it takes to update the grid. This way all the threads get a chance to exit.
Naumaan
May 22nd, 2006, 05:59 AM
In dialog close event, you can set a boolean variable or an event and sleep for maximum time it takes to update the grid. This way all the threads get a chance to exit.Furthermore u can use GetExitCodeThread api to determine either ur thread is strill alive or not. Untill GetExitCodeThread not succedded u can check thread status again and again after a particular sleep time. Check MSDN for further Information.
Andreas Masur
May 22nd, 2006, 09:18 AM
Furthermore u can use GetExitCodeThread api to determine either ur thread is strill alive or not. Untill GetExitCodeThread not succedded u can check thread status again and again after a particular sleep time. Check MSDN for further Information.
There is no need for doing that...the same can be achieved by waiting on the thread handle (which doesn't involves CPU cycles and function calls)... ;)
Andreas Masur
May 22nd, 2006, 09:19 AM
You are right. I have used events to close the thread. When dialog box gets closed one event will be set through which thread can return from controlling function. But if this GUI thread is in the function which updates GUI, and if I try to close the application, thread is not returning from that function. So event will be of no use for it. In that way thread is getting blocked there. I think you have understood the problem.
Yes, however, how does the thread updates the GUI? Using messages?
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.