64,683
社区成员
发帖
与我相关
我的任务
分享
void CTest_WhileDlg::OnButton1()
{
// TODO: Add your control notification handler code here
int i=1;
while (i<10000)
{
i=i+1;
Sleep(100);
}
}
void CTest_WhileDlg::OnButton1()
{
// TODO: Add your control notification handler code here
int i=1;
MSG msg;
while (i<10000)
{
i=i+1;
Sleep(100);
if (0==i%10)
if (GetMessage(&msg,NULL,0,0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
//只是一个简单fix, 自己改进
bool shutdown = false;
unsigned int __stdcall ThreadFun(PVOID pM)
{
int i=1;
while (!shutdown && i<10000)
{
i=i+1;
Sleep(100);
}
AfxMessageBox("执行完毕");
return 0;
}
void CTest_WhileDlg::OnButton1()
{
HANDLE handle=(HANDLE)_beginthreadex(NULL,0,ThreadFun,NULL,0,NULL);
CloseHandle(handle); //不要等待,也不要循环,只是简单的调度一下
}
//新加一个按钮,在关闭程序前, 关闭子线程
void CTest_WhileDlg::OnButton2()
{
shutdown = true;
}
WaitForSingleObject(handle,INFINITE); //这里还是相当于循环,变了一个形式而已
//子线程函数
unsigned int __stdcall ThreadFun(PVOID pM)
{
int i=1;
while (i<10000)
{
i=i+1;
Sleep(100);
}
AfxMessageBox("执行完毕");
return 0;
}
void CTest_WhileDlg::OnButton1()
{
//创建一个新的子线程,运行自己想要的代码
HANDLE handle=(HANDLE)_beginthreadex(NULL,0,ThreadFun,NULL,0,NULL);
WaitForSingleObject(handle,INFINITE);
}
while(true)
{
peak next message();
process message() //调用XXX::OnButton1(...),这个函数一直运行,
//后面的消息一直得不到处理,因为只有一个UI线程。
}