首先声明, 这里的工作线程与UI线程是相对的,即没有任何窗口的. 如果需要与主线程或其它辅助线程通讯,有几种方法如事件,消息,信号等,也可以是以上几种方法的综合运用.下面就列出以下3种通讯方法的代码框架
(1)只用消息通讯
- DWORD ThreadProc(LPVOID lParam)
- {
-
- MSG msg;
- PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
-
- SetEvent(hEvent);
-
- while(true)
- {
- GetMessage(&msg, NULL, 0, 0);
- switch(msg.message)
- {
- case WM_QUIT:
- return 1;
-
-
- case WM_USER + 100:
- break;
-
-
- case WM_USER + 101:
- break;
- }
- }
- return 0;
- }
(2)只用事件通讯
- DWORD ThreadProc(LPVOID lParam)
- {
- DWORD dwIndex;
- while (true)
- {
- dwIndex = WaitForMultipleObjects(cObjects, pObjects, FALSE, INFINTE);
- if (WAIT_OBJECT + 0== dwIndex)
- {
- return 1;
- }
- else if (WAIT_OBJECT + 1 == dwIndex)
- {
-
- }
- else if (WAIT_OBJECT + cObjects - 1 == dwIndwx)
- {
-
- }
- }
- }
(3)用消息和事件通讯
- DWORD ThreadProc(LPVOID lParam)
- {
- while (TRUE)
- {
- DWORD ret ;
- MSG msg ;
-
- while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
- {
- switch(msg.message)
- {
-
- case WM_QUIT:
- return 1;
-
-
- case WM_USER + 100:
- break;
-
- case WM_USER + 101:
- break;
- }
- }
- ret = MsgWaitForMultipleObjects(cObjects, lphObjects, FALSE,INFINITE,QS_POSTMESSAGE);
- if (ret == (WAIT_OBJECT_0 + cObjects))
- {
-
- continue;
- }
- else
- {
-
- if (ret == WAIT_OBJECT_O)
- {
- }
- else if (ret == WAIT_OBJECT_O + 1)
- {
- }
- else if(ret == WAIT_OBJECT_O + cObjects - 1)
- {
- }
- }
- return 0;
- }
上面用到了GetMessage和PeekMessage 函数, 这两者都是从消息队列取出消息, 不同的是GetMessage从消息队列删除消息,并且阻塞调用线程. PeekMessage则是查询消息队列,如果有消息就取出,没有消息也立即返回, 是否从消息队列删除消息由最后一个参数决定:PM_REMOVE表示删除,PM_NOREMOVE表示不删除.可以简单地认为,GetMessage是同步的,PeekMessage是异步的。
*******************************************************************************************************
除以上作者所提到的线程通信方法外还有一种利用MFC消息循环通信的方法,其操作过程如下:
1.从CWnd派生类CSendDataWnd
- #define WM_SENDDATA WM_USER+100
- #define WM_POSTDATA WM_USER+101
-
- class CSendDataWnd : public CWnd
- {
- public:
- CSendDataWnd();
-
- protected:
-
- LRESULT OnSendData(WPARAM wParam, LPARAM lParam);
- LRESULT OnPostData(WPARAM wParam, LPARAM lParam);
-
- DECLARE_MESSAGE_MAP()
- };
-
-
- BEGIN_MESSAGE_MAP(CSendDataWnd, CWnd)
- ON_MESSAGE(WM_SENDDATA, OnSendData)
- ON_MESSAGE(WM_POSTDATA, OnPostData)
- END_MESSAGE_MAP()
-
- CSendDataWnd::CSendDataWnd() {
-
- }
-
- LRESULT CSendDataWnd::OnSendData(WPARAM wParam, LPARAM lParam)
- {
- switch(wParam) {
-
- }
- return 0;
- }
-
- LRESULT CSendDataWnd::OnPostData( WPARAM wParam, LPARAM lParam )
- {
- switch(wParam) {
-
- }
- return 0;
- }
2.声明CSendDataWnd变量
- CSendDataWnd m_wndSendData;
3.调用CreateEx创建窗口
- if (!m_wndSendData.CreateEx(0, AfxRegisterWndClass(0),
- _T("SendData Notification window"),
- WS_OVERLAPPED, 0, 0, 0, 0, NULL, NULL)) {
- TRACE("Create SendData Notification window error!\n");
- AfxThrowResourceException();
- }
4.向CSendDataWnd窗口发送处理消息
- m_wndSendData.SendMessage(WM_SENDDATA, wParam, lParam);
-
- m_wndSendData.PostMessage(WM_POSTDATA, wParam, lParam);
5.使用完毕后,在适当位置调用DestroyWindow销毁消息窗口防止内存泄露
- m_wndSendData.DestroyWindow();