Solved

Worker thread and AfxGetMainWnd

Posted on 2004-09-19
3
996 Views
Last Modified: 2013-11-20
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
Comment
Question by:Lescha
3 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 50 total points
ID: 12095133
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12097101
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
 
LVL 1

Author Comment

by:Lescha
ID: 12099681
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now