Solved

Worker thread and AfxGetMainWnd

Posted on 2004-09-19
3
1,008 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do you programatically show and hide the Windows 10 On-Screen-Keyboard? 3 517
List out all word 7 254
Hibernate methods 2 67
Path of Workbook 3 59
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

932 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

11 Experts available now in Live!

Get 1:1 Help Now