Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1129
  • Last Modified:

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?
 
0
Lescha
Asked:
Lescha
1 Solution
 
AlexFMCommented:
You can read more about this problem here:

http://support.microsoft.com/default.aspx?scid=kb;EN-US;147578

CWnd-derived objects may be used only in the thread where they was created. Don't call directly any function of CWnd-derived class from the worker thread. Intead of this post user-defined message to the window.
Check out MFC sample MTRECALC.
0
 
Jaime OlivaresCommented:
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);
0
 
LeschaAuthor Commented:
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.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now