如何实时通知子线程让其关闭?
另一个贴:
http://community.csdn.net/Expert/topic/3757/3757242.xml?temp=.0368616
谁有例子啊?
UINT MyThread( LPVOID lParam )
{
while(true);
return 0;
}
void MyDeal(void* vParam)
{
HANDLE hThread;
hThread=AfxBeginThread((AFX_THREADPROC)MyThread, (LPVOID)vParam);
if(hThread==NULL)
AfxMessageBox("Error!");
return;
}
void CTestView::OnButton1()
{
//开始
char *str1 = new char[10];
_beginthread(MyDeal, 0, (void*)(str1) );
}
void CTestView::OnButton2()
{
//结束
}
问题点数:20、回复次数:4Top
1 楼yanedanny(守望者)回复于 2005-01-27 16:17:57 得分 5
子线程的while里面放检查消息的非堵塞代码,如果收到某特定消息就退出while。 通知线程可以postthreadmessage该特定消息给子线程。子线程id可以用全局变量保存。Top
2 楼delphihero(何去何从)回复于 2005-01-27 16:30:06 得分 10
while(true)--->
HANDLE m_hEventKill;
m_hEventKill = CreateEvent(NULL, TRUE, FALSE, NULL);while(WaitForSingleObject(m_hEventKill,50)==WAIT_TIMEOUT)
{
}
关闭时:
SetEvent(m_hEventKill);Top
3 楼chuiyun(吹云)回复于 2005-01-27 16:50:00 得分 0
我可能需要强制关闭线程,因为用了ReadDirectoryChangesW
bMon为全局变量,
我在OnButton1() 里将其置为true然后启动线程,在OnButton2() 里将其置为false,让线程自己退出,但不成功,因为"暂停"在ReadDirectoryChangesW上了.
while(bMon)
{
//ReadDirectoryChangesW)
//此处可能会等待很久,所以要强制关闭线程
}Top
4 楼oyljerry(【勇敢的心】→ ㊣提拉米苏√㊣)回复于 2005-01-27 19:54:31 得分 5
用标志退出比较好Top




