Click to See Complete Forum and Search --> : about end a thread


wangwangwangwang
December 22nd, 2004, 01:15 AM
i use a Event and a BOOL value to stop a thread, the thread is create with CreateThread, and a loop in the thread, when i create the thread,
i set the event object to non signal and the bool value to FALSE, when i click another button ,want to stop it, i set the bool value to TRUE to break the loop,then before quit the thread ,i set the Event. code such as below:
Button1Click:
m_bStop = FALSE;
ResetEvent(m_hEvent);
CreateThread(..);
In the Thead:(a loop in it)
{..
while (!m_bStop)
{
..
}
SetEvent(m_hEvent);
return 0;
}

Button2Click:
{
m_bStop = TRUE;
WaitForMultipleObjects(1,&m_hEventStop,TRUE,INFINITE);
..
}
but when i click the button 2, the application will be like deadlock,why?
and how to solve it?

thank you
}

saiprabhur
December 22nd, 2004, 02:13 AM
Use SyncObjects to avoid Dead lock Refer MSDN.

It will help you to come out from DeadLock.


Regards

Sai

Andreas Masur
December 22nd, 2004, 04:53 AM
[ Moved thread ]

Andreas Masur
December 22nd, 2004, 04:56 AM
but when i click the button 2, the application will be like deadlock,why?
and how to solve it?


Well...the only thing I can see here is simply that you do not need 'WaitForMultipleObjects()' here. Thus...what is 'm_hEventStop'? Take a look at the following FAQ (http://www.codeguru.com/forum/showthread.php?t=305166)...