• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 478
  • Last Modified:

get the current document from a different thread

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
aikimen
Asked:
aikimen
  • 2
  • 2
  • 2
1 Solution
 
JackThorntonCommented:
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
 
peterchen092700Commented:
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
 
aikimenAuthor Commented:
I will try to splite the pointes for both the comments.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
aikimenAuthor Commented:
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
 
JackThorntonCommented:
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
 
peterchen092700Commented:
...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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now