UI线程和工作者线程需要考虑同步问题吗?
本帖最后由 kxltsuperr 于 2013-03-11 18:21:36 编辑 我在CSDN高手的帮助下,写了一段小代码(MFC exe),如下:
//子线程函数
unsigned int __stdcall ThreadFun(PVOID pM)
{
int i=1;
while (i<50)
{
i=i+1;
Sleep(100);
}
AfxMessageBox("执行完毕");
return 0;
}
void CTest_WhileDlg::OnButton1()
{
//创建一个新的子线程,运行自己想要的代码
HANDLE handle=(HANDLE)_beginthreadex(NULL,0,ThreadFun,NULL,0,NULL);
CloseHandle(handle);
}
HANDLE handle=(HANDLE)_beginthreadex(NULL,0,ThreadFun,NULL,0,NULL);,高手们分析了这样会阻塞消息,不能加WaitForSingleObject(handle,INFINITE);这一句。
WaitForSingleObject(handle,INFINITE);