Solved

PostMessage() from CView to CDocument

Posted on 2013-12-18
2
460 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
2 Comments
 
LVL 33

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

860 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