问题描述
好吧,我无疑尝试了这种天真的尝试,以创建具有新窗口和消息循环的新线程.
我有一个必须打开窗口的功能.处理它的消息,并且必须在可以从应用程序中调用它的环境中运行,而没有现有的消息循环(也没有其他窗口),通常的mfc消息循环或WTL消息循环.
我看到了一些有关AddMessageLoop和Modules的东西吗?但似乎是针对主要应用程序的.无论如何,那里可能没有WTL模块.需要一个带有基本消息循环的独立窗口.
传递尚未打开窗口的WTL类,因此窗口在与循环相同的线程中打开.
Ok, I tried this no doubt naive attempt to create a new thread with a new window and message loop.
I have a function that must open a window & process its messages, and must run in environments where it may be called from application with no existing message loop (and no other windows) or the usual mfc message loop or a WTL message loop.
I saw some stuff about AddMessageLoop and Modules? but it appeared that was for the main application. In any case, there may or may not be a WTL module out there. Need a stand alone window with a basic message loop.
Passing in a WTL class with window not yet opened, so Window opens in same thread as loop.
// does not work....
static DWORD WINAPI MyRunThread(__in LPVOID lpParameter)
{
CMessageLoop theLoop;
WTLclass *nav = (WTLclass *) lpParameter;
nav->CreateWindow();
int nRet = theLoop.Run();
return nRet;
}
CreateThread(0,0,MyRunThread,&nav,0,0);
推荐答案
static DWORD WINAPI MyRunThread(__in LPVOID lpParameter)
{
CMessageLoop theLoop;
WTLsubclass *nav = (WTLsubclass *) lpParameter;
nav->CreateWindow();
SetEvent(WindowCreatedEvent) // signal event HANDLE type for worker thread
int nRet = theLoop.Run();
return nRet;
}
//CreateThread(0,0,MyRunThread,&nav,0,0); // & wont work on WTL class objects
// some genius overloaded the & operator on WTL class objects, just because in C++ you can
//Workaround to get address of WTL class object
CLASSnav nav[1];
CLASSnav *pnav = nav; // because arrays is address of first element.
CreateThread(0,0,MyRunThread,pnav,0,0);
这篇关于世界贸易组织线程,如何在新线程中创建窗口和消息循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!