如何等待在其他线程中创建窗口

Ste*_*ila 0 c++ oop winapi

我有以下代码:

class Service {
public:
    void start();
    void stop();
private:
    HANDLE hThread;
    HWND   hWindow;
};

void Service::start() {
    hThread = CreateThread(...); // creates window and goes on to message loop
}

void Service::stop() {
    // !!! wait for m_hwnd to become valid
    // send signal to thread to stop
    PostMessage(m_hwnd, WM_CLOSE, 0, 0);
    // wait to thread to die
    ::WaitForSingleObject(hThread, INFINITE);
    CloseHandle(hThread);
}
Run Code Online (Sandbox Code Playgroud)

客户代码:

Service obj;
obj.start();
obj.stop();
Run Code Online (Sandbox Code Playgroud)

问题是当主线程调用时stop(),子线程还没有创建窗口,并且没有消息循环来处理WM_CLOSE消息.我该如何等待创建窗口?功能WaitOnAddress似乎做我需要的,但它是win8和鞋面,我需要一些关于winxp级别的东西

Jon*_*ter 5

事件对象是线程间/进程间通信的一种形式.它们让一个线程在继续之前等待另一个线程中发生任意事件.

基本上你想要这样的东西:

class Service {
    HANDLE hThread;
    HANDLE hEvent;
    HWND   hWindow;
};

void Service::start() {
    // create event
    hEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr); 

    hThread = CreateThread(...); // creates window and goes on to message loop

    // wait for window
    WaitForSingleObject(hEvent, INFINITE);
    CloseHandle(hEvent);
}


void thread_function(...)
{
    // create window, etc

    // signal parent to continue
    SetEvent(hEvent);
}
Run Code Online (Sandbox Code Playgroud)