使用PostThreadMessage传递消息

  PostThreadMessage的原型是这样的
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());
        
return1;
    }
    
    
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());
        
return1;
    }

    
//start thread
    hThread = (HANDLE)_beginthreadex( NULL,0, &fun, NULL,0, &nThreadID );
    
if(hThread== 0)
    {
        printf(
"start thread failed,errno:%d\n",::GetLastError());
        CloseHandle(hStartEvent);
        
return1;
    }

    
//wait thread start event to avoid PostThreadMessage return errno:1444
    ::WaitForSingleObject(hStartEvent,INFINITE);
    CloseHandle(hStartEvent);

    
int count= 0;
    
while(true)
    {
        
char* pInfo= newchar[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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在异步线程中使用PostMessagePostThreadMessage发送消息,可以向指定的窗口或线程发送消息,让其在消息循环中处理。一般情况下,我们可以自定义特定的消息类型来传递数据或通知事件。 具体实现可以参考以下步骤: 1. 定义消息类型,可以使用WM_APP+数字来定义自定义消息类型,例如#define WM_MY_MESSAGE (WM_APP + 1)。 2. 使用PostMessagePostThreadMessage发送消息,其中PostMessage用于向指定的窗口发送消息PostThreadMessage用于向指定的线程发送消息。例如,可以使用以下代码向指定窗口发送消息: ``` HWND hWnd = ::FindWindow(NULL, _T("Window Title")); if (hWnd != NULL) { ::PostMessage(hWnd, WM_MY_MESSAGE, WPARAM wParam, LPARAM lParam); } ``` 3. 在窗口或线程的消息循环中处理自定义消息。 对于窗口,可以在WndProc函数中添加对自定义消息的处理,例如: ``` LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_MY_MESSAGE: // 处理自定义消息 break; // 其他消息处理 } return DefWindowProc(hWnd, message, wParam, lParam); } ``` 对于线程,可以在线程函数中使用GetMessage或PeekMessage函数获取消息并处理,例如: ``` DWORD WINAPI ThreadFunc(LPVOID lpParam) { MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { if (msg.message == WM_MY_MESSAGE) { // 处理自定义消息 } else { TranslateMessage(&msg); DispatchMessage(&msg); } } return 0; } ``` 需要注意的是,PostMessagePostThreadMessage是异步发送消息,即发送后不会等待消息处理完毕再返回。如果需要同步发送消息并等待处理完毕再返回,可以使用SendMessage或SendMessageTimeout函数。同时,由于异步发送消息可能会导致消息被快速发送过多,因此在发送消息前应该加入消息队列,保证消息的顺序和稳定性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值