Solved

PostMessage() from CView to CDocument

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FMX enumerated colours 2 84
Having a different time zone on your task bar 3 53
Need more details 5 128
Header Errors LNK2019, LNK1120 - Unresolved Externals 4 97
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…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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 learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

863 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

24 Experts available now in Live!

Get 1:1 Help Now