Solved

Worker thread and AfxGetMainWnd

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Process filename extension 3 194
NotAlone Challenge 20 84
How to convert MFC::CString to UTF8 wchar_t* 10 276
firstChar challenge 13 119
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…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.

809 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