zoukankan      html  css  js  c++  java
  • PostThreadMessage使用

    函数原型:

    BOOL WINAPI PostThreadMessage(_In_ DWORD idThread,_In_ UINT Msg,_In_ WPARAM wParam,_In_ LPARAM lParam);

    idThread -      [in] Type: DWORD The identifier of the thread to which the message is to be posted.

    Msg     -       [in] Type: UINT The type of message to be posted.

    wParam -        [in] Type: WPARAM Additional message-specific information.

    lParam -        [in] Type: LPARAM Additional message-specific information.

     

    1、创建worker thread,worker thread中包括消息循环。

    2、worker thread发送TALK_MESSAGE,弹出提示“Worker Thread

    3、worker thread发送WM_QUIT, worker thread将报告给主线程自己要退出了,然后结束自己的生命周期。

    DWORD ThreadProc(LPVOID lParam)
    {
            MSG msg;
            while(GetMessage(&msg,0,0,0))
            {
                    if(msg.message == TALK_MESSAGE)
                    {
                            MessageBox(NULL,L"Hi",L"Worker Thread",MB_OK);
                    }
                    DispatchMessage(&msg);
            }
    
            MessageBox(NULL,L"Thread will close by pressing OK",L"From Worker Thread",MB_OK);
            AfxGetApp()->m_pMainWnd->PostMessageW(TALK_MESSAGE+1,0,0);
            return 0;
    }
    
    void CPostThreadMSGDlg::OnBnClickedOk()
    {
            CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadProc,0,0,&m_dwThread);
            ::MessageBox(NULL,L"Worker Thread Created!",L"From main Thread",MB_OK);
            OnOK();
    } 
    
    void CPostThreadMSGDlg::OnBnClickedButtonHi()
    {
            PostThreadMessage(m_dwThread,TALK_MESSAGE,0,0);
    }
    
     
    
    void CPostThreadMSGDlg::OnBnClickedButtonCllose()
    {
            PostThreadMessage(m_dwThread,WM_QUIT,0,0);
    }
    
     
    
    LONG CPostThreadMSGDlg::OnWorkerThreadQuitFunction(WPARAM wParam, LPARAM lParam)
    {     
            ::MessageBox(NULL,L"Main thread have known Worker Thread died!",L"From main Thread",MB_OK);
    
            return 0;
    }
    

     

     

  • 相关阅读:
    ES6的reduce( )方法 可以数组求和、数组去重、二维数组转一维数组、计算数组中每个元素出现的次数
    防止/禁止页面后退
    CSS预处理器SASS将迁移到Dart Sass
    FormData 对象的使用
    javascript中编码与解码的decodeURI()、decodeURIComponent()区别
    Chrome谷歌浏览器调试技巧小结
    javaweb:tomcat&servlet
    java:Druil连接池
    java:c3p0连接池的使用
    java:JDBC的使用方式
  • 原文地址:https://www.cnblogs.com/licb/p/2654476.html
Copyright © 2011-2022 走看看