Solved

PostMessage() from CView to CDocument

Posted on 2013-12-18
2
484 Views
Last Modified: 2013-12-19
In my C++ MFC SDI document/view application, how do I use PostMessage() to send a request from a CView object to it's corresponding CDocument object?

I need the CView object to tell the CDocument object to do something special without the CView object having to wait on the CDocument object to finish processing the request via a standard function call.

I know how to use PostMessage() to send a message from a CView object to the CMainFrame object?

As a temporary solution, I have the CView object PostMessage() to the CMainFrame object, who then calls a function of the CWinApp object, who then calls a function of the CDocment object. That works but seems like there should be a more direct way to do this.
0
Comment
Question by:robear7nt
[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
2 Comments
 
LVL 34

Accepted Solution

by:
sarabande earned 500 total points
ID: 39728732
the CDocument is a windows-less class and therefore it can't receive or handle messages.

because of that, the document class is not suitable to run "asynchronously". the workaround you made by posting to the mainframe (window) is not really running asynchronously as the view window and the mainframe window are running in the same message loop. so a post message actually was processed only delayed in the loop but if doing a lengthy operation, it would "freeze" the view same as you would call the document from view directly cause no other messages could be processed while the document function doesn't return. if you have the impression that the call of PostMessage is better than calling the document function directly from view, it probably is because of the delay that was caused by the PostMessage call. to spare the detour via mainframe and application you could send the message to the own view. then a view member function could handle the message and call GetDocument()->function from that.

if you need to run it really asynchronously, there are there are 3 ways:
(1) you set up a timer by SetTimer and add the OnTimer member function to the view. then, the document function should be rewritten such that it has no lengthy parts but could be called for each step separately.  for example if the function contains of a loop you would remove the loop and  pass the loop counter and loop data as argument. then, the OnTimer  would call the function multiple times and avoids lengthy operation with no handling of messages.
(2) in the document's function you run a further message loop such that the normal windows messages like mouse or keyboard messages could be processed while the control is in the function.
while (GetMessage(...))
{
        TranslateMessage(...);
        DispatchMessage(...);
}

Open in new window

the additional message loop is somewhat dangerous as some messages (for example click to exit button) could destroy the current objects like view and document. so you could not process all messages but would return from function (thus passing control to normal message loop) on some messages.
(3) in the document's function you create a thread which does the lengthy parts. then, the document's function could return immediately after creating the thread. note, the thread safely could use PostMessage calls to view or frame window, but never SendMessage calls as it runs asynchronously.

in sum, if you only need the delay you may send a message to the view. if the document's function has some parts which could run separately, you may use the OnTimer. if the code is too lengthy and badly can be parted you would use the thread solution as it is the most clearest and well proved.

Sara
0
 
LVL 1

Author Closing Comment

by:robear7nt
ID: 39730796
Sara,

You provided an excellent explanation (and more thoroughly than anything I'd found searching the web). Your possible solutions were also well explained.

Thank you very much for your time & attention in answering my question.

Robert
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

688 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