Posted on 2012-10-19 09:43
盛胜 阅读(234)
评论(0) 编辑 收藏 引用
void CMulThreadDlg::OnBnClickedButtonTheadsametime()
{
// TODO: 在此添加控件通知处理程序代码
CWinThread *p1=AfxBeginThread(thread1event,this,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
// 线程1 优先级与主线程共同 挂起线程直到RusumeThread
p1->ResumeThread();
eventto.SetEvent();//CEvent eventto
CWinThread *p2=AfxBeginThread(thread2event,this,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
p2->ResumeThread();
}
UINT thread1event( LPVOID pParam )
{
CMulThreadDlg *eve1=(CMulThreadDlg *)pParam;
while (1)
{
WaitForSingleObject(eventto.m_hObject,INFINITE);
/*
hHandle:指定对象或事件的句柄;
dwMilliseconds: 等待时间,以毫妙为单位,当超过等待时间时,此函数将返回。如果该参数设置为0,则该函数立即返回,如果设置为INFINITE,则该函数直到有信号才返回。
*/
eve1->GetDlgItem(IDC_STATIC1)->SetWindowText("线程1打开");
Sleep(5000);
eve1->GetDlgItem(IDC_STATIC1)->SetWindowText("线程1关闭");
eventto.SetEvent();
if (1==m_stop1)
{
m_stop1=0;
break;
}
}
return 0;
}
UINT thread2event( LPVOID pParam )
{
CMulThreadDlg *eve2=(CMulThreadDlg *)pParam;
while(1)
{
WaitForSingleObject(eventto.m_hObject,INFINITE);
eve2->GetDlgItem(IDC_STATIC2)->SetWindowText("线程2打开");
Sleep(4000);
eve2->GetDlgItem(IDC_STATIC2)->SetWindowText("线程2关闭");
eventto.SetEvent();
if (1==m_stop2)
{
m_stop2=0;
break;
}
}
return 0;
}
void CMulThreadDlg::OnBnClickedButtonClosethread()
{
// TODO: 在此添加控件通知处理程序代码
m_stop1=1;
m_stop2=1;
}