线程通信

线程通信:CThreadDome文件名
1.通过全局变量
2.发消息
    2.1给窗口发消息
 #define SEND_DATA  (WM_USER+1)//定义处理消息的宏
 UINT _cdecl CThreadDomeThreadDlg::Tread(LPVOID lpvoid)//线程函数
 {
  CThreadDomeDlg *pThis=(CThreadDomeDlg*)lpvoid;
  while(1)
  {
   if(g_count!=0)
   {
    int sum=0;
    for(int i=0;i<=g_count;i++)
    {
     sum+=i;
    }
    pThis->PostMessage(SEND_DATA,(WPARAM)sum);//向窗口发消息,添加处理消息的ON_MESSAGE
    break;
   }
  }
  return 0;
 }
 int g_count=0;//全局变量实现线程间通信
 void CThreadDomeDlg::OnBnClickedButton1()//启动线程
 {
  AfxBeginThread(&CThreadDomeDlg::Tread,this);
 }
 void CThreadDomeDlg::OnBnClickedButton2()//计算按钮处理事件
 {
  UpdateData();
      if(m_Sum==0)//m_Sum 是radio控件添加的变量
  {
   g_count=100;
  }
   if(m_Sum==1)
  {
   g_count=1000;
  }
   if(m_Sum==2)
  {
   g_count=1000;
  }
 }
 ON_MESSAGE(SEND_DATA,&CThreadDomeDlg::OnUser)
 afx_msg LRESULT CThreadDomeDlg::OnUser(WPARAM wParam,LPARAM lParam)
 {
  str=(int)wParam;//str是Edit控件添加的int类型value变量
  UpdateData(FALSE);
  return 0;
 }
 //-------------------------------------------SendMessage与PostMessage区别--------------------------------------------------------
 SendMessage不走消息队列,等待处理完成回来在执行
 PostMessage放入消息队列,不知道什么时候执行
 //--------------------------------------------------------------------------------------------------------------------------------------------------
 //-------------------------------------------PeekMessage与GetMessage区别--------------------------------------------------------
 //--------------------------------------------------------------------------------------------------------------------------------------------------
 2.2给线程发消息
 给UI线程发消息,在线程类中添加消息映射表
 在线程类中
 afx_msg void OnThread(WPARAM wParam,LPARAM lParam);//更改了向向窗口显示的过程
 ON_THREAD_MESSAGE(SEND_DATA,&CThreadDomeApp::OnThread)
  void CThreadDomeApp::OnThread(WPARAM wParam,LPARAM lParam)
 {
  ((CThreadDomeDlg*)m_pMainWnd)->str=(int)wParam;
  m_pMainWnd->UpdateData(FALSE);
 }
 线程调用PostThreadMessage
 AfxGetApp()->PostThreadMessage(SEND_DATA,(WPARAM)sum,0);
 2.3给工作者线程发消息
 .h定义CWinThread *m_Thread;
 #define UM_WORKER (WM_USER+2)
 #define UM_QUIT (WM_USER+3)
 void CThreadDomeDlg::OnBnClickedButton1()
 {
  m_Thread=AfxBeginThread(&CThreadDomeThreadDlg::Tread,this);
 }
 void CThreadDomeDlg::OnBnClickedButton2()
 {
  UpdateData();
  int g_count=0;
  if(m_Sum==0)
  {
   g_count=100;
  }
   if(m_Sum==1)
  {
   g_count=1000;
  }
    if(m_Sum==2)
  {
   g_count=1000;
  }
  m_Thread->PostThreadMessage(UM_WORKER,WPARAM(g_count),0);//向工作者线程发消息
 }
 
 UINT _cdecl CThreadDomeThreadDlg::Tread(LPVOID lpvoid)
 {
  CThreadDomeThreadDlg *pThis=(CThreadDomeThreadDlg*)lpvoid;
  while(1)
  {
   MSG msg;
   //自己取消息
   if(::PeekMessage(&msg,NULL,0,0,PM_REMOVE))
   {
    if(UM_QUIT==msg.message)
    {
     break;
    }
    if(UM_WORKER==msg.message)
    {
     int sum=0;
     for(int i=0;i<=(int)msg.wParam;i++)
     {
      sum+=i;
     }
     AfxGetApp()->PostThreadMessage(SEND_DATA,(WPARAM)sum,0);
     continue;
    }
   }
  }
  return 0;
 }
 在Dlg类中添加OnClose消息
 void CThreadDomeDlg::OnClose()
 {
  m_Thread->PostThreadMessage(UM_QUIT,0,0);//防止线程泄漏
  CDialogEx::OnClose();
 }


 
 
 
 
 
 
 
 


 

你可能感兴趣的:(线程通信)