1

我编写了一个多线程 WTL 实用程序来对内部服务进行压力测试。

Comms 线程向主线程发出他们已经退出的信号,因此主线程可以删除它们对应的对象。

他们发出这样的信号:

PostThreadMessage(m_dwParentThreadId, WM_THREADQUIT, 1, m_dwNetThreadId);

我的问题是如何处理我定义的自定义消息。

WM_THREADQUIT is #define'd as WM_USER + 10

我想使用消息映射中的条目来调用处理程序,例如:

BEGIN_MSG_MAP(CMainDlg)
  MESSAGE_HANDLER( WM_INITDIALOG, OnInitDialog )
  MESSAGE_HANDLER( WM_THREADQUIT, OnThreadQuit )
  ...
  REFLECT_NOTIFICATIONS()
END_MSG_MAP()

但是,永远不会调用 OnThreadQuit。

我可以处理它的唯一方法是在 PreTranslateMessage 中显式调用处理程序:

virtual BOOL CMainDlg::PreTranslateMessage(MSG* pMsg)
{
  if( pMsg->message == WM_THREADQUIT )
  {
    BOOL blHandled;
    OnThreadQuit(pMsg->message, pMsg->wParam, pMsg->lParam, blHandled);
    return TRUE;
  }
  return CWindow::IsDialogMessage(pMsg);
}

我确定这不是正确的方法...

我很想知道正确的方法-有人可以帮忙吗!?

4

1 回答 1

1

As stated in the doc Messages sent by PostThreadMessage are not associated with a window. As a general rule, messages that are not associated with a window cannot be dispatched by the DispatchMessage function.

Set your HWND in pMsg->hwnd and ::DispatchMessage() will deliver it to your WndProc:

virtual BOOL CMainDlg::PreTranslateMessage(MSG* pMsg) 
{ 
  if (pMsg->message == WM_THREADQUIT) 
  { 
    pMsg->hwnd = m_hWnd; 
    return FALSE; // continue processing
  } 
  return CWindow::IsDialogMessage(pMsg); 
}
于 2010-03-06T00:12:08.997 回答