Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


get the current document from a different thread

Posted on 2001-08-14
Medium Priority
Last Modified: 2013-11-20
I have an MDI application and I'm trying to use an additional thread to perform some heavy tasks.
But in my thread I fail to get the active child frame with the method MDIGetActive (it returns NULL).

I am aware of the thread local storage of MFC and I have tried to change the module state but with no success.

Does any know how to get to my document from within my thread?

Question by:aikimen
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
  • 2
  • 2
  • 2

Accepted Solution

JackThornton earned 400 total points
ID: 6386060
You generally cannot *directly* access/manipulate GUI objects from outside the thread that created them. The internal Windows GUI mechanisms are not, for whatever reason, threadsafe.

Some things you can do:

(a) make a "global" pointer to the current document, updated from within your GUI thread.

(b) give your thread class a method that allows the GUI thread to "register" the current document when it changes. This method would copy appropriate names/pointers to a member variable, and can be protected with a critical section. Better solution than (a), if for no other reason than I hate globals ;-)

(c) if your worker thread has a message loop, post a thread message to your thread whenever the current (topmost) document changes.

No matter how you go about it, your GUI thread will have to take some of the responsibility of passing information to your worker thread.

- jack

Expert Comment

ID: 6386705
Jack: GUI Objects and Windows are different things (in this respect). Windows (as the OS) Windows (as the colorful thingies) serialize their messages through the queue - no matter from what thread they come. MFC, however, is a different issue

aikimen: two important things:

a) MFC's doc/view architecture is not threadsafe, so you have to synchronize access to your document class on your own. Do you know how to work with Critical Sections (& the like)?

b) Best is, the UI thread passes the document to the worker thread. Use one of the suggestions Jack suggested, or (my favourite) : If you use AfxBeginThread to create for a worker thread, just pass the CDocument * as pParam.

You are responsible to make sure the worker thread doesn't exist (or at least, doesn't access the CDocument anymore) when the document object is deleted.


Author Comment

ID: 6387134
I will try to splite the pointes for both the comments.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 6387150
I have tried splitting the points but it looks I did not succeed doing so. I appreciate both answers.
I willing to give peterchen his share of points but I don't know how. Any suggestions are welcome.


Expert Comment

ID: 6389283
You can make an "empty" question with a subject like "points for peterchen" and however many points you want to award. When he answers, you can award the points.

One point I was trying to make vis-a-vis Windows & the GUI is that, for example, you cannot seem to SendMessage from one thread to a window created by another thread or you get what appears to be a deadly embrace. That means the supposed "serialization" method/technique/implementation that intercepts the call, puts the message on the target queue, then returns the answer to the caller is not thread safe.

- jack

Expert Comment

ID: 6390247
...to split the points, you neet to post a request on the Community Support opage, since EE staff must detract some of your points here.

jack: from MSDN, SendMessage docs
However, the sending thread will process messages from its queue while waiting for its message to be processed.
This prevents the deadly embrace

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

618 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