Solved

PostMessage() from CView to CDocument

Posted on 2013-12-18
2
437 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 32

Accepted Solution

by:
sarabande earned 500 total points
Comment Utility
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
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

771 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

9 Experts available now in Live!

Get 1:1 Help Now