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.
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf


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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

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: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

752 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