Click here to Skip to main content
15,885,875 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
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.
C#
// 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);
Posted
Updated 20-Apr-12 15:05pm
v2
Comments
Sergey Alexandrovich Kryukov 20-Apr-12 22:33pm    
From this post, not quite clear what's the problem.
--SA

Please take a look at this CodeProject article:
A WTL Game Loop[^].

—SA
 
Share this answer
 
Comments
mrbll 21-Apr-12 10:53am    
Thanks, I read the article.

It appears to still depend on the global _Module, and not say anything about adding separate windows in separate threads with separate message loops.

My problem is how to run a WTL window in an environment where there may be no global _Module. This is supposed to be ok after WTL 7, but I can't find any examples of how to Run a WTL message loop without a global _Module.
Thanks to several people on several forums who supplied helpful answers to this and several spawned questions. The real problem as all too often was something completely different.

See comments for additions needed.
Gotcha #1 was & operator does not work on WTL classes
Gotcha #2, is never name your events. If you link with unknown other objects,
its likely someone already used the same name, and Windows decides that the same names means the same event, even though multiple CreateEvent() calls are use. No Name,
no "aliasing" of supposedly different events.

Non-Gotcha, if you DON'T want the "main" message handler handling your messages, you don't need _Module or to "tell" anything about your message loop. If you want independent
window with independent loop, see below.
===================================================
C++
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);
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900