MFC中多线程传参数解决方案2015-02-01 19:17:34

( 还没有投票,继续加油! )
分享: ╬收藏
31.3K

VC++ MFC中多线程传参原来如此简单,参考如下代码:


typedef struct THREAD_PARAM
{
  HWND hWnd;
  int nData;
}_THREAD_PARAM;
m_pThread = AfxBeginThread(ThreadProc, &m_ThreadParam,
THREAD_PRIORITY_ABOVE_NORMAL, 0, CREATE_SUSPENDED);
CDemoDlg::~CDemoDlg()
{
  if (m_pThread != NULL)
  {
    //等待线程结束
    ::WaitForSingleObject(m_pThread->m_hThread, INFINITE);
    delete m_pThread;
    m_pThread = NULL;
  }
}
//调用参数
  THREAD_PARAM* pThreadParam = (THREAD_PARAM*)pParam;
  pThreadParam->nData++;
//向主线程窗口发送消息
::PostMessage(pThreadParam->hWnd, WM_THREADMSG, 0, 0);
////////////////////////////////////////////////Again
LRESULT CDemoDlg::OnMsgFunc()
{
  SetDlgItemInt(IDC_DATA, m_ThreadParam.nData);
  return 1;
}
void CDemoDlg::OnBeginThread()
{
  m_ThreadParam.hWnd = m_hWnd;
  //启动线程,初始为挂起状态
  AfxBeginThread(ThreadProc, &m_ThreadParam);
}
UINT ThreadProc(LPVOID pParam)
{
  //线程参数
  THREAD_PARAM* pThreadParam = (THREAD_PARAM*)pParam;
  for (int n = 0; n < 10; n++)
  {
    Sleep(100);
    pThreadParam->nData++;
    //向主线程窗口发送消息
  // ::PostMessage(pThreadParam->hWnd, WM_THREADMSG, 0, 0);
  }
  return 0;
}
//THREAD_PARAM m_hThreadParam;THREAD_PARAM m_ThreadParam;






头像

snowcoal
  • MFC
  • 多线程
  • 传参

本文标签:

MFC多线程传参

收藏到我的私密空间

标题:MFC中多线程传参数解决方案

作者:柳岸花明

你暂未登录,请登录后才可收藏至您的私密空间 确认取消
雪炭网

键盘操作 更便捷 -雪炭网雪中送炭-乐趣无限