使用PostThreadMessage在Win32线程间传递消息
来源:互联网 发布:手机股票训练软件 编辑:程序博客网 时间:2024/04/30 23:17
使用PostThreadMessage在Win32线程间传递消息
Posted on 2005-12-31 15:31 小明 阅读(16527) 评论(7) 编辑 收藏 引用 所属分类: Win32PostThreadMessage的原型是这样的
BOOL PostThreadMessage( DWORD idThread,
UINT Msg,
WPARAM wParam,
LPARAM lParam
);
PostThreadMessage可以用于线程之间的异步通讯,因为它不用等待调用者返回,
这也许是线程通讯中最简单的一种方法了。
但是要注意以下问题
1 .PostThreadMessage有时会失败,报1444错误(Invalid thread identifier. )
其实这不一定是线程不存在的原因,也有可能是线程不存在消息队列(message queue)造成的。
事实上,并不是每个thread都有message queue,那如何让thread具有呢?
答案是,至少调用message相关的function一次,比如GetMessage,PeekMessage。
2.如果是post动态分配的memory给另外一个thread,要注意内存的正确释放。
3.PostThreadMessage不能够post WM_COPYDATE之类的同步消息,否则会报错
4.最好不要使用PostThreadMessage post message给一个窗口,使用PostMessage替代。
下面是我写的一个比较严整的例子,仅供参考。
#include <windows.h>
#include <cstdio>
#include <process.h>
#define MY_MSG WM_USER+100
const int MAX_INFO_SIZE = 20;
HANDLE hStartEvent; // thread start event
// thread function
unsigned __stdcall fun(void *param)
{
printf("thread fun start\n");
MSG msg;
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
if(!SetEvent(hStartEvent)) //set thread start event
{
printf("set start event failed,errno:%d\n",::GetLastError());
return 1;
}
while(true)
{
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;
}
}
};
return 0;
}
int main()
{
HANDLE hThread;
unsigned nThreadID;
hStartEvent = ::CreateEvent(0,FALSE,FALSE,0); //create thread start event
if(hStartEvent == 0)
{
printf("create start event failed,errno:%d\n",::GetLastError());
return 1;
}
//start thread
hThread = (HANDLE)_beginthreadex( NULL, 0, &fun, NULL, 0, &nThreadID );
if(hThread == 0)
{
printf("start thread failed,errno:%d\n",::GetLastError());
CloseHandle(hStartEvent);
return 1;
}
//wait thread start event to avoid PostThreadMessage return errno:1444
::WaitForSingleObject(hStartEvent,INFINITE);
CloseHandle(hStartEvent);
int count = 0;
while(true)
{
char* pInfo = new char[MAX_INFO_SIZE]; //create dynamic msg
sprintf(pInfo,"msg_%d",++count);
if(!PostThreadMessage(nThreadID,MY_MSG,(WPARAM)pInfo,0))//post thread msg
{
printf("post message failed,errno:%d\n",::GetLastError());
delete[] pInfo;
}
::Sleep(1000);
}
CloseHandle(hThread);
return 0;
}
#include <cstdio>
#include <process.h>
#define MY_MSG WM_USER+100
const int MAX_INFO_SIZE = 20;
HANDLE hStartEvent; // thread start event
// thread function
unsigned __stdcall fun(void *param)
{
printf("thread fun start\n");
MSG msg;
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
if(!SetEvent(hStartEvent)) //set thread start event
{
printf("set start event failed,errno:%d\n",::GetLastError());
return 1;
}
while(true)
{
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;
}
}
};
return 0;
}
int main()
{
HANDLE hThread;
unsigned nThreadID;
hStartEvent = ::CreateEvent(0,FALSE,FALSE,0); //create thread start event
if(hStartEvent == 0)
{
printf("create start event failed,errno:%d\n",::GetLastError());
return 1;
}
//start thread
hThread = (HANDLE)_beginthreadex( NULL, 0, &fun, NULL, 0, &nThreadID );
if(hThread == 0)
{
printf("start thread failed,errno:%d\n",::GetLastError());
CloseHandle(hStartEvent);
return 1;
}
//wait thread start event to avoid PostThreadMessage return errno:1444
::WaitForSingleObject(hStartEvent,INFINITE);
CloseHandle(hStartEvent);
int count = 0;
while(true)
{
char* pInfo = new char[MAX_INFO_SIZE]; //create dynamic msg
sprintf(pInfo,"msg_%d",++count);
if(!PostThreadMessage(nThreadID,MY_MSG,(WPARAM)pInfo,0))//post thread msg
{
printf("post message failed,errno:%d\n",::GetLastError());
delete[] pInfo;
}
::Sleep(1000);
}
CloseHandle(hThread);
return 0;
}
===========Happy New Year==============
- 使用PostThreadMessage在Win32线程间传递消息
- 使用PostThreadMessage在Win32线程间传递消息
- 使用PostThreadMessage在Win32线程间传递消息
- 使用PostThreadMessage在Win32线程间传递消息
- 使用PostThreadMessage在Win32线程间传递消息 (zhuan)
- 使用PostThreadMessage在Win32线程间传递消息
- 使用PostThreadMessage在Win32线程间传递消息
- 使用PostThreadMessage在Win32线程间传递消息
- [转贴]使用PostThreadMessage在Win32线程间传递消息
- 使用PostThreadMessage在Win32线程间传递消息
- 使用PostThreadMessage在Win32线程间传递消息
- 使用PostThreadMessage在Win32线程间传递消息
- 使用PostThreadMessage在Win32线程间传递消息
- 使用PostThreadMessage在Win32线程间传递消息
- Hook : 使用PostThreadMessage在Win32线程间传递消息
- 使用PostThreadMessage在Win32线程间传递消息
- 使用PostThreadMessage在Win32线程间传递消息
- 使用PostThreadMessage在Win32线程间传递消息
- 提高你的Java代码质量吧:优先使用整形池
- 第一个Objective-C 程序
- linux系统的性能问题排除分析
- android 进程和线程
- 2013-07-24 Chapter2 UIApplication、UIWindow、 UIViewController、 UIView
- 使用PostThreadMessage在Win32线程间传递消息
- HTML标签内的连接、文本、垂直居中 兼容绝大部分浏览器
- 我是程序员,我自豪
- 使用linux配置dhcp v6服务器
- CentOS 静态IP 路由
- Java环境下Memcached应用详解
- jQuery获取文档及可视宽高、高度
- VC Webbrowser 控件窗口的隐藏和显示
- 神奇的i=i++