windows线程消息通信和处理 PostThreadMessage和PeekMessage GetMessage

通过下面的代码我们会了解到:
  1. 除了UI线程,普通的线程也可以接受其他线程发来的消息,并处理消息。寄送消息用PostThreadMessage,接受消息用GetMessage。
  2. 消息发送到接收线程的message queue消息队列里面。这里要注意一点,线程一上来有时候还没来得及产生消息队列,发送线程调用PostThreadMessage会失败,发送线程可以售后再发,或者接受线程预先条用PeekMessage来强制产生消息队列。
  3. 请类比win32或者mfc的消息处理过程,win32是主线程while循环接收并处理消息的。
// MessagePosting.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <process.h>

#define MY_MSG WM_USER+100 
const int MAX_INFO_SIZE = 20;
HANDLE hStartEvent; // thread start event

// thread function
unsigned __stdcall ReceiveMessage(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, &ReceiveMessage, 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; 
}

 

参考:http://www.cnblogs.com/ahuo/archive/2007/08/22/864948.html

转载于:https://www.cnblogs.com/dirichlet/archive/2011/03/18/1988377.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值