wxsignal.h 1.2 KB
Newer Older
1 2 3 4 5 6 7 8
#pragma once
#include <windows.h>

#define SIGNAL(id, uMsg) PostThreadMessage(id, uMsg, 0, 0)

class WxSignal
{
public:
L
ljc545w 已提交
9
    WxSignal(UINT uMsg, DWORD &dwThread)
10 11
    {
        this->hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)this->Pump, (LPVOID)uMsg, 0, &this->threadId);
L
ljc545w 已提交
12
        dwThread = this->threadId;
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    }
    ~WxSignal()
    {
        if (this->hThread)
            CloseHandle(this->hThread);
        SIGNAL(this->threadId, WM_QUIT);
    }
    static void Pump(UINT uMsg)
    {
        MSG msg = {0};
        int ret;
        while ((ret = GetMessage(&msg, NULL, 0, 0)) != 0)
        {
            if (msg.message == uMsg)
            {
                break;
            }
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    void wait(int timeout)
    {
        if (this->hThread)
        {
            WaitForSingleObject(this->hThread, timeout);
            PostThreadMessage(this->threadId, WM_QUIT, 0, 0);
            CloseHandle(this->hThread);
            this->hThread = NULL;
        }
    }
    DWORD GetThreadId()
    {
        return this->threadId;
    }

private:
    HANDLE hThread;
    DWORD threadId;
};