BOOL runflag; //这个为否正在发送数据的标志
int uiButton; //这个是表明哪一个函数启动的发送线程 的标志
HANDLE m_CANEvent;
HANDLE m_CANSendThreadHandle[3]; //这里定义3个handle 实际只用一个
CWinThread *pCANThreads;
BOOL CExample2Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
…
…
m_CANEvent=CreateEvent(NULL,FALSE,FALSE,NULL); //声明一个事件
runflag=0; //线程运行标志置0
return TRUE; // return TRUE unless you set the focus to a control
}
void CExample2Dlg::OnButton1() //这是一个按钮响应函数,用按钮开启线程的函数
{
DWORD result ;
MSG msg ;
if(runflag==1) //如果线程正在运行
{
SetEvent(m_CANEvent); //发出让线程结束
while(1)
{
result=MsgWaitForMultipleObjects(1,m_CANSendThreadHandle,FALSE,INFINITE,QS_ALLINPUT);
if (result == WAIT_OBJECT_0) //如果等到线程结束 就执行下面的break;
{
break;
}
else
{
while (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) //线程没有结束 就分发消息
{
TRACE("收到消息,函数返回值为%d /n",result);
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
}
ResetEvent(m_CANEvent); //设置无信号,让线程进入等待超时的工作
uiButton=1; //标记这是按钮响应的函数启动的线程
pCANThreads=AfxBeginThread(MyThread1,this);
m_CANSendThreadHandle[0]=pCANThreads->m_hThread; //赋值handle
runflag=1;
Sleep(1000); //故意延迟1秒 让开启的线程跑一跑
}
UINT CExample2Dlg::MyThread1(void *param)
{
CExample2Dlg *Dlg=(CExample2Dlg *)param;
int i;
CString tmp;
DWORD dw;
//在进入下面循环前,在我那个真实程序前,还有不少Dlg->XXXX的工作,在线程里面操作主界面的一//些显示,读取数据等工作
for(i=0;i<100000;i++) //这里是我人为模拟 要让它循环这么多次,意为模拟发送这么多次
{
dw = WaitForSingleObject(Dlg->m_CANEvent,10); //让其延迟10ms就输出一个数
if( dw == WAIT_OBJECT_0 ) //收到结束线程信号
{
Dlg->runflag=0; //线程运行标志复位
return 0;
}
if( dw == WAIT_TIMEOUT) //延迟到了,就执行下面的显示数值
{
if(Dlg->uiButton == 1)
{
tmp.Format(" %d ",i);
Dlg->m_count.SetWindowText(tmp); //这个是让其在界面上面显示当前i的数值
} // Dlg->m_count是主界面上面的一个static控件
if(Dlg->uiButton ==2 )
{
tmp.Format(" %d ",i);
Dlg->m_count2.SetWindowText(tmp); //这个是让其在界面上面显示当前i的数值
} // Dlg->m_count是主界面上面的一个static控件
}
}
Dlg->runflag=0; //线程运行标志复位
return 0;
}
void CExample2Dlg::OnButton2() //停止线程的按钮
{
// TODO: Add your control notification handler code here
SetEvent(m_CANEvent);
}
else if( WAIT_OBJECT_0 + 1)
{
while (PeekMessage(&msg,NULL,0,0,PM_NOREMOVE))
{
TRACE("收到消息,函数返回值为%d,进入消息分发 \n",result);
TRACE("message里面消息值 %d \n",msg.message);
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
收到消息,函数返回值为1,进入消息分发
message里面消息值 512
BOOL CExample2Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
…
…
m_CANEvent=CreateEvent(NULL,FALSE,FALSE,NULL); //声明一个事件
runflag=0; //线程运行标志置0
return TRUE; // return TRUE unless you set the focus to a control
}
void CExample2Dlg::OnButton1() //用按钮开启线程的函数
{
// TODO: Add your control notification handler code here3
DWORD result ;
MSG msg ;
if(runflag==1) //如果线程正在运行
{
SetEvent(m_CANEvent); //发出让线程结束
TRACE("发送SetEvent信号,让线程结束 \n");
while (TRUE)
{
//wait for m_hThread to be over,and wait for
//QS_ALLINPUT(Any message is in the queue)
DWORD dwRet = MsgWaitForMultipleObjects (1, m_CANSendThreadHandle, FALSE, INFINITE, QS_ALLINPUT);
switch(dwRet)
{
case WAIT_OBJECT_0:
{
TRACE("MsgWaitForMultipleObjects 返回值 %ld ",dwRet);
TRACE("从WAIT_OBJECT_0消息退出while循环 \n");
}
break; //break the loop
case WAIT_OBJECT_0 + 1:
//get the message from Queue
//and dispatch it to specific window
{
TRACE("MsgWaitForMultipleObjects 返回值 %ld ",dwRet);
TRACE("说明返回WAIT_OBJECT_0 + 1 ,分发消息,继续while循环\n");
PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
DispatchMessage(&msg);
}
continue;
default:
{
TRACE("MsgWaitForMultipleObjects 返回值 %ld ",dwRet);
TRACE("从default退出循环 \n");
}
break; // unexpected failure
}
break;
}
}
ResetEvent(m_CANEvent); //设置无信号,让线程进入等待超时的工作
uiButton=1; //标记这是按钮响应的函数启动的线程
pCANThreads=AfxBeginThread(MyThread1,this);
m_CANSendThreadHandle[0]=pCANThreads->m_hThread;
runflag=1;
Sleep(1000);
}
UINT CExample2Dlg::MyThread1(void *param)
{
TRACE("MyThread1 Begin\n");
CExample2Dlg *Dlg=(CExample2Dlg *)param;
int i;
CString tmp;
DWORD dw;
for(i=0;i<100000;i++)
{
dw = WaitForSingleObject(Dlg->m_CANEvent,10); //让其延迟10ms就输出一个数
if( dw == WAIT_OBJECT_0 ) //收到结束线程信号
{
Dlg->runflag=0; //线程运行标志复位
TRACE("MyThread1 End 1\n");
return 0; //退出
}
if( dw == WAIT_TIMEOUT) //延迟到了,就执行下面的显示数值
{
if(Dlg->uiButton == 1)
{
tmp.Format(" %d ",i);
Dlg->m_count.SetWindowText(tmp); //这个是让其在界面上面显示当前i的数值
}
if(Dlg->uiButton ==2 )
{
tmp.Format(" %d ",i);
Dlg->m_count2.SetWindowText(tmp); //这个是让其在界面上面显示当前i的数值
}
}
}
Dlg->runflag=0; //线程运行标志复位
TRACE("MyThread1 End\n");
return 0;
}
29.41 KB, 下载次数: 0
实验程序源代码
void CExample2Dlg::OnButton1() //用按钮开启线程的函数
{
// TODO: Add your control notification handler code here3
DWORD result ;
MSG msg ;
if(runflag==1) //如果线程正在运行
{
SetEvent(m_CANEvent); //发出让线程结束
TRACE("发送SetEvent信号,让线程结束 \n");
while (TRUE)
{
//wait for m_hThread to be over,and wait for
//QS_ALLINPUT(Any message is in the queue)
DWORD dwRet = MsgWaitForMultipleObjects (1, m_CANSendThreadHandle,FALSE, INFINITE, QS_ALLINPUT);
if(result == WAIT_OBJECT_0)
{
TRACE("MsgWaitForMultipleObjects 返回值 %ld 从WAIT_OBJECT_0消息退出while循环 \n",dwRet);
break; //break the loop
}
else if(result == WAIT_OBJECT_0 + 1)
{
//get the message from Queue
//and dispatch it to specific window
TRACE("MsgWaitForMultipleObjects 返回值 %ld 说明返回WAIT_OBJECT_0 + 1 ,分发消息,继续while循环\n",dwRet);
PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
DispatchMessage(&msg);
continue;
}
else if( result == WAIT_ABANDONED_0 )
{
TRACE("MsgWaitForMultipleObjects 返回值 %ld 说明返回WAIT_ABANDONED_0 ,跳出while\n",dwRet);
break;
}
else
{
TRACE("MsgWaitForMultipleObjects 返回值 %ld 从default退出循环 \n",dwRet);
DWORD dErrCode=GetLastError();
TRACE("dErrCode=%ld \n",dErrCode);
break; // unexpected failure
}
}
}
ResetEvent(m_CANEvent); //设置无信号,让线程进入等待超时的工作
uiButton=1; //标记这是按钮响应的函数启动的线程
pCANThreads=AfxBeginThread(MyThread1,this);
m_CANSendThreadHandle[0]=pCANThreads->m_hThread;
runflag=1;
}
pCANThreads=AfxBeginThread(MyThread1,this);
m_CANSendThreadHandle[0]=pCANThreads->m_hThread;
void CExample3Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
DWORD result ;
MSG msg ;
if(runflag==1) //如果线程正在运行
{
SetEvent(m_CANEvent); //发出让线程结束
TRACE("发送SetEvent信号,让线程结束 \n");
while (TRUE)
{
//wait for m_hThread to be over,and wait for
//QS_ALLINPUT(Any message is in the queue)
result = MsgWaitForMultipleObjects (1, m_CANSendThreadHandle,FALSE, INFINITE, QS_ALLINPUT);
if(result == WAIT_OBJECT_0)
{
TRACE("MsgWaitForMultipleObjects 返回值 %ld 从WAIT_OBJECT_0消息退出while循环 \n",result);
break; //break the loop
}
else if(result == WAIT_OBJECT_0 + 1)
{
//get the message from Queue
//and dispatch it to specific window
TRACE("MsgWaitForMultipleObjects 返回值 %ld 说明返回WAIT_OBJECT_0 + 1 ,分发消息,继续while循环\n",result);
while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
//afxDump << msg.message << "\n";
TRACE("分发的消息ID=0x%x \n",msg.message);
TranslateMessage(&msg);
DispatchMessage(&msg);
}
continue;
/*
PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
DispatchMessage(&msg);
continue;
*/
}
else if( result == WAIT_ABANDONED_0 )
{
TRACE("MsgWaitForMultipleObjects 返回值 %ld 说明返回WAIT_ABANDONED_0 ,跳出while\n",result);
break;
}
else
{
TRACE("MsgWaitForMultipleObjects 返回值 %ld 从default退出循环 \n",result);
DWORD dErrCode=GetLastError();
TRACE("dErrCode=%ld \n",dErrCode);
break; // unexpected failure
}
}
}
ResetEvent(m_CANEvent); //设置无信号,让线程进入等待超时的工作
uiButton=1; //标记这是按钮响应的函数启动的线程
m_CANSendThreadHandle[0]=CreateThread(NULL,0,MyThread3,this,0,NULL);
runflag=1;
}
DWORD WINAPI CExample3Dlg::MyThread3(LPVOID param)
{
TRACE("MyThread1 Begin\n");
CExample3Dlg *Dlg=(CExample3Dlg *)param;
int i;
CString tmp;
DWORD dw;
for(i=0;i<100000;i++)
{
dw = WaitForSingleObject(Dlg->m_CANEvent,10); //让其延迟10ms就输出一个数
if( dw == WAIT_OBJECT_0 ) //收到结束线程信号
{
Dlg->runflag=0; //线程运行标志复位
TRACE("MyThread1 End 1\n");
return 0; //退出
}
if( dw == WAIT_TIMEOUT) //延迟到了,就执行下面的显示数值
{
if(Dlg->uiButton == 1)
{
tmp.Format(" %d ",i);
Dlg->m_count.SetWindowText(tmp); //这个是让其在界面上面显示当前i的数值
}
if(Dlg->uiButton ==2 )
{
tmp.Format(" %d ",i);
Dlg->m_count2.SetWindowText(tmp); //这个是让其在界面上面显示当前i的数值
}
}
}
Dlg->runflag=0; //线程运行标志复位
TRACE("MyThread1 End\n");
return 0;
}
欢迎光临 重工电子论坛 (http://cqutlab.cn/) | Powered by Discuz! X3.1 |