使用PostThreadMessage向工作线程发送消息
来源:互联网 发布:音乐截取软件 for mac 编辑:程序博客网 时间:2024/06/10 14:58
参考文章:http://blog.csdn.net/liuliu20036/article/details/3879152
http://blog.chinaunix.net/uid-20596472-id-1918307.html
把window线程间传送消息仔细的看了一遍,觉得以前的理解很不深刻。说一说对PostThreadMessage的理解。
PostThreadMessage是一个线程体发送一个消息到指定的线程ID,其原型如下:
BOOL PostThreadMessage(
DWORD idThread,
UINT Msg,
WPARAM wParam,
LPARAM lParam
);
这个函数既可以发送消息给工作线程,也可以发送给UI线程。接受PostThreadMessage的线程必须已经有了一个message queue,否则调用PostThreadMessage会失败。因为此原因使用GetLastError会得到错误码为1444,这种情况经常出现,解决方法有如下两种:
1. 调用PostThreadMessage,如果失败,就Sleep一段时间再次调用PostThreadMessage直到调用成功;
2. 创建一个Event对象,让PostThreadMessage等待接受的线程创建一个message queue。可以通过调用PeekMessage强制系统创建一个message queue。示例代码如下:
假设mainAPP是发送线程ThreadA是接受线程
/*mainAPP.cpp*/
……
hStartEvent = ::CreateEvent(0,FALSE,FALSE,0); //create thread start event
if(hStartEvent == 0)
{
printf("create start event failed,errno:%d/n",::GetLastError());
return 1;
}
::WaitForSingleObject(hStartEvent,INFINITE);
CloseHandle(hStartEvent);
if(!PostThreadMessage(threadaID, WM_MESSAGE_A,0,0))
{
_tprintf(_T("post error! %d/n"), GetLastError());
return 1;
}
……
ThreadA是接受线程
/* ThreadA */
MSG msg;
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
if(!SetEvent(hStartEvent))
{
printf("set event error,%d/n",GetLastError());
return 1;
}
while(true){
if(GetMessage(&msg, 0,0,0)) {
switch(msg.message){
case WM_MESSAGE_A:
……
break;
}
}
}
}
PostThreadMessage传递的消息如果要包含信息,要注意在结束的时候释放消息中的信息。在消息中附加信息方法如下
/*构造信息如下*/
char* pInfo = new char[MAX_INFO_SIZE]; //create dynamic msg
sprintf(pInfo,"msg_%d",++count);
PostThreadMessage(nThreadID,MY_MSG,(WPARAM)pInfo,0)//post thread msg
/*解释信息如下*/
if(GetMessage(&msg,0,0,0)) //get msg from message queue
{
switch(msg.message)
{
case MY_MSG:
char * pInfo = (char *)msg.wParam;
printf("recv %s/n",pInfo);
delete[] pInfo; //这里释放了资源
break;
}
}
做了一个简单的消息通信实验,让主线程中等待用户输入,产生不同的消息,并把这些消息post给子线程,子线程根据产生的消息做出不同的反映。这些子线程可以是工作线程也可以是UI线程。
#include "stdafx.h"#include <Windows.h>#include <process.h>#include <cstdio>#define MY_MSG WM_USER + 100const int MAX_INFO_SIZE = 20;HANDLE g_hEvent;HANDLE m_hThread;unsigned int m_nThreadId;unsigned _stdcall ThreadFunc(LPVOID lpParam){ printf("thread fun start/n"); MSG msg; PeekMessage(&msg,NULL,WM_USER,WM_USER,PM_NOREMOVE); if(!SetEvent(g_hEvent)) //set thread start event { printf("Set Event fail: %d",GetLastError()); return 0; } while(true) { if(GetMessage(&msg,NULL,0,0))//get msg from message queue { switch (msg.message) { case MY_MSG: char* pInfo = (char*)msg.wParam; printf("recv %s",pInfo); printf("\n"); delete [] pInfo; break; } } } return 0;}int _tmain(int argc, _TCHAR* argv[]){g_hEvent = CreateEvent(NULL,FALSE,FALSE,NULL);//create thread start eventif(g_hEvent == NULL){printf("Create Event Fail: %d",GetLastError());return 0;} //start threadm_hThread =(HANDLE)_beginthreadex(NULL,0,ThreadFunc,NULL,0,&m_nThreadId);if(m_hThread == NULL){printf("Create New Thread fail :%d",GetLastError());return 0;}//wait thread start event to avoid PostThreadMessage return errno:1444WaitForSingleObject(g_hEvent,INFINITE);CloseHandle(g_hEvent);int count = 0;while(true){char* pInfo = new char[MAX_INFO_SIZE];//create dynamic msgsprintf(pInfo,"msg_%d",++count);if(!PostThreadMessage(m_nThreadId,MY_MSG,(WPARAM)pInfo,0))//post thread msg{printf("post message failed,errno: %d/n",GetLastError());delete [] pInfo;}if(count >= 20){break;}Sleep(1000);}CloseHandle(m_hThread);getchar();return 0;}
- 使用PostThreadMessage向工作线程发送消息
- 使用PostThreadMessage向工作线程发送消息
- 使用PostThreadMessage向工作线程发送消息
- 使用PostThreadMessage向工作线程发送消息
- 使用PostThreadMessage向工作线程发送消息
- 使用PostThreadMessage向工作线程发送消息
- 使用PostThreadMessage向工作线程发送消息
- 使用PostThreadMessage向发送线程消息失败的解决方法
- 向线程投递消息PostThreadMessage
- 使用PostThreadMessage发送消息遇到的问题
- 向线程发送消息
- 向线程发送消息
- 向线程发送消息
- 向线程函数内投递消息CWinThread::PostThreadMessage
- Handler(15):工作线程向主线程发送消息
- 工作线程中利用handler向主线程发送消息
- PostThreadMessage 线程的消息
- PostThreadMessage发送进程间消息
- iOS 8 系统自带BlurEffect 毛玻璃特效
- 实例变量的初始化时机
- 旧电脑重装系统
- [LeedCode OJ]#198 House Robber
- hdoj 1232 畅通工程【并查集】
- 使用PostThreadMessage向工作线程发送消息
- C++虚拟继承与普通继承的区别
- Wireshark软件使用教程(上部) 相关选项解释
- ios信息推送 异常 javax.net.ssl.SSLHandshakeException: Received fatal alert: handshak
- 【NPR】Unity3D非真实感渲染----铅笔画滤镜
- jquery整理
- linux下在grub.cfg中配置win7启动引导
- 深入理解java异常处理机制
- Codeforces Round #317 (Div. 2) (572A Arrays,572B Order Book)