<menu id="w8yyk"><menu id="w8yyk"></menu></menu>
  • <dd id="w8yyk"><nav id="w8yyk"></nav></dd>
    <menu id="w8yyk"></menu>
    <menu id="w8yyk"><code id="w8yyk"></code></menu>
    <menu id="w8yyk"></menu>
    <xmp id="w8yyk">
    <xmp id="w8yyk"><nav id="w8yyk"></nav>
  • 網站首頁 > 物聯資訊 > 技術分享

    windows平臺發消息到非UI線程.

    2016-09-28 00:00:00 廣州睿豐德信息科技有限公司 閱讀
    睿豐德科技 專注RFID識別技術和條碼識別技術與管理軟件的集成項目。質量追溯系統、MES系統、金蝶與條碼系統對接、用友與條碼系統對接 下面的代碼是介紹如何在windows平臺發消息到非UI線程. 
    主要是'PeekMessage || GetMessage' 這兩個API的應用. 當他們被調用的時候,如果當前線程還沒有消息循環,就會創建一個.
    利用這個特性比自己手動的去創建一個消息循環要方便得多. 
    發消息主要是使用線程PostThreadMessage

    1. #include <iostream>
    2. #include <string>
    3. #include "cassert"
    4. #include "windows.h"
    5. #include "process.h"

    6. enum { MSG_TEST = WM_USER+100,MSG_EXIT };

    7. unsigned __stdcall fun(void *param)
    8. {
    9.     MSG msg;

    10.     while(true)
    11.     {
    12.         if(GetMessage(&msg,0,0,0)) //get msg from message queue
    13.         {
    14.             char * info = reinterpret_cast<char*>(msg.wParam);
    15.             bool keep_in_loop = true;
    16.             switch(msg.message)
    17.             {
    18.             case MSG_TEST:
    19.                 std::cout << info << std::endl;
    20.                 break;
    21.             case MSG_EXIT:keep_in_loop=false;
    22.                 std::cout << info << std::endl;
    23.                 break;
    24.             default: break;
    25.             }


    26.             if ( ! keep_in_loop )
    27.             {
    28.                 break;
    29.             }
    30.         }
    31.     }
    32.     std::cout << "out of loop" << std::endl;
    33.     return 0;
    34. }


    35. void main()
    36. {
    37.     HANDLE hThread;
    38.     unsigned nThreadID;

    39.     //start thread
    40.     hThread = (HANDLE)_beginthreadex( NULL, 0, &fun, NULL, 0, &nThreadID );

    41.     while(true)
    42.     {
    43.         UINT Msg = MSG_TEST;
    44.         const char * p = "MSG_TEST";

    45.         std::string commond;
    46.         std::cin >> commond;
    47.         if ( commond == "exit" )
    48.         {
    49.             Msg = MSG_EXIT;
    50.         }

    51.         BOOL bPostOK = PostThreadMessage(nThreadID,Msg,(WPARAM)p,0);
    52.         if ( ! bPostOK )
    53.         {
    54.             assert( false );
    55.             // the post event is to too early, please build msg loop first
    56.             // 'PeekMessage || GetMessage' will forced to build the msg loop, if it does not exist.
    57.         }
    58.     }
    59. }

    RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成
    最近免费观看高清韩国日本大全