?
Solved

Worker thread and AfxGetMainWnd

Posted on 2004-09-19
3
Medium Priority
?
1,103 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 200 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

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

765 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