Solved

PostMessage() from CView to CDocument

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

749 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