C++ 如何等待在其他线程中创建窗口
我有以下代码:C++ 如何等待在其他线程中创建窗口,c++,oop,winapi,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() {
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);
}
客户端代码:
Service obj;
obj.start();
obj.stop();
问题是当主线程调用stop()
时,子线程还没有创建窗口,并且没有消息循环来处理WM\u CLOSE
消息。我应该如何等待窗口被创建?
函数WaitOnAddress
似乎满足了我的需要,但它是win8和更高版本,我需要一些关于winxp级别的信息是线程间/进程间通信的一种形式。它们让一个线程等待另一个线程中发生任意事件,然后再继续
基本上你想要这样的东西:
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);
}
是线程间/进程间通信的一种形式。它们让一个线程等待另一个线程中发生任意事件,然后再继续
基本上你想要这样的东西:
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);
}
@不幸的是,它没有使用同步对象(例如事件)。@JonathanPotter你能详细说明一下吗?你有新帐户吗。我猜你问@Tas yep,忘了发邮件,lol@MNS,遗憾的是,它没有使用同步对象(例如事件)。@JonathanPotter你能详细说明一下吗?你有新帐户吗。我猜你问@Tas是的,忘了发电子邮件,哈哈