void CMultiThread9Dlg::OnBnClickedWritew()
{
//创建子线程1
CWinThread *pWriteD=AfxBeginThread(WriteD,
&m_ctrlD,
THREAD_PRIORITY_HIGHEST,
0,
0);
//创建子线程2
CWinThread *pWriteW=AfxBeginThread(WriteW,
&m_ctrlW,
THREAD_PRIORITY_HIGHEST,
0,
0);
//如果去掉此循环,线程1,2可以正常运行,在编辑框里出现数字
//为什么要等待主线程执行之后才执行子线程。明明子线程优先级高于主线程。
//感谢您的关注。
while(1)
{
Sleep(100);
}
}
UINT WriteW(LPVOID pParam)
{
CEdit *pEdit=(CEdit*)pParam;
CString str;
for(int i=0;i<10;i++)
{
str.Format(_T("%d"),i);
pEdit->SetWindowText(str);
Sleep(100);
}
eventWriteD.SetEvent();
return 0;
}
UINT WriteD(LPVOID pParam)
{
CEdit *pEdit=(CEdit*)pParam;
WaitForSingleObject(eventWriteD,INFINITE);
CString str;
for(int i=0;i<10;i++)
{
str.Format(_T("%d"),i);
pEdit->SetWindowText(str);
Sleep(100);
}
return 0;
}