Link to home
Start Free TrialLog in
Avatar of Lescha
Lescha

asked on

Worker thread and AfxGetMainWnd

I have the following piece of code:

CWnd *CMyApp::GetView()
{
  CMainFrame *pFrm = (CMainFrame *)AfxGetMainWnd();
  return pFrm->GetActiveView();
}

It works fine from the main thread of the application. For some reason, I cannot call this function from a worker thread.

I have heard there is a problem when AfxGetMainWnd is called from a worker thread. Is this correct, and if so, why and what kind of problems?
 
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Still you can send standard window messages if you want to communicate with a view.
Something like:

theApp.m_pMainWnd->SendMessage(WM_USER+1, somedata, somedata);
Avatar of Lescha
Lescha

ASKER

The problem was that I forgot to create the thread as suspended, set its Main Window pointer, and then resume.
Still, it would be unfair to deny the answer points to Alex.
James - just for the record, SendMessage in the way you suggested crashes the application in Release mode. Dunno why.