Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ WTL threads
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);
Posted 20-Apr-12 15:58pm
mrbll183
Edited 20-Apr-12 16:05pm
RaisKazi33.1K
v2
Comments
SAKryukov at 20-Apr-12 22:33pm
   
From this post, not quite clear what's the problem.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Please take a look at this CodeProject article:
A WTL Game Loop[^].
 
—SA
  Permalink  
Comments
mrbll at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
===================================================
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);
  Permalink  
v3

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

  Print Answers RSS
0 OriginalGriff 520
1 Maciej Los 290
2 Richard MacCutchan 265
3 BillWoodruff 235
4 Suraj Sahoo | Coding Passion 155
0 OriginalGriff 8,804
1 Sergey Alexandrovich Kryukov 7,457
2 DamithSL 5,689
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411028.1 | Last Updated 22 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100