Solved

get the current document from a different thread

Posted on 2001-08-14
6
393 Views
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?

Thanks.
0
Comment
Question by:aikimen
  • 2
  • 2
  • 2
6 Comments
 
LVL 3

Accepted Solution

by:
JackThornton earned 100 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
0
 
LVL 7

Expert Comment

by:peterchen092700
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.

Peter
0
 

Author Comment

by:aikimen
ID: 6387134
I will try to splite the pointes for both the comments.
0
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.

 

Author Comment

by:aikimen
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.


Thanks.
0
 
LVL 3

Expert Comment

by:JackThornton
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.

Peter:
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
0
 
LVL 7

Expert Comment

by:peterchen092700
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
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
Tab names are off by one 5 53
Shell script to login to remote m/c and execute commands 10 78
dog bark java program 15 87
modThree challenge 4 76
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

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