Re: PostMessage and unprocessed messages
"Doug Harrison [MVP]" <dsh@mvps.org> ha scritto nel messaggio
news:2336t353et13j9gcpv43argob25jcat1c3@4ax.com...
On Sat, 8 Mar 2008 09:10:14 +0100, "Giovanni Dicanio"
<giovanni.dicanio@invalid.com> wrote:
However, I must confess that this [WFSO] seems to me not the best
solution...
In fact using WFSO I'm blocking (even if for small time) the main GUI
thread, and I think this is not very good (for example, the GUI thread
won't
process important messages like WM_PAINT, if my understanding is
correct...).
It won't process any messages while stuck in WFSO.
OK, as I thought.
See also what I wrote about using MsgWaitForMultipleObjects here (Q5-Q7):
http://members.cox.net/doug_web/threads.htm#Q5
Thanks Doug.
As you also recently wrote about CString/std::string and COW, there is no a
better solution for everything, for all cases, and the right answer is "it
depends".
I think that the WFSO technique is simple (at least for my case), and worked
fine.
I also think that using an external class to manage dynamic data allocation
and deletion for messages is fine for me.
In fact, I would just discard the pending messages if everything would fit
inside WPARAM and LPARAM. Instead, I must process them (the pending
messages) just to do the "delete <my-pointer-from-lParam>".
But, if I have the centralized external class (with methods New/Delete/Clear
for 1) create new message data on the heap, 2) delete it, 3) delete all
[pending] message data), when the dialog box is closed, the dialog-box
parent can just call MessageMemoryManager.Clear() to delete pending message
data.
It's something like this:
// ... in WinMain (or somewhare in dialog parent)
MessageMemoryManager mgr;
// Create the dialog and run it, and pass reference to msg memory manager
MyDialog d;
d.SetMsgMemoryManager( &mgr );
d.DoModal();
// The dialog terminated. We can clear all pending messages (if any, so no
memory leak)
mgr.Clear();
The sender thread (worker background thread) in dialog-box, does:
Message * data = MessageMemoryManager.New();
... set data fields
... and put data in lParam
PostMessage(...) to main GUI thread
The receiver (GUI thread) message handler does:
// extract data from lParam
Message * data = (Message *)lParam;
... process data
// Release it
MessageMemoryManager.Delete( data );
Note that all creation and deletion are done using the MessageMemoryManager
class (this class monitors also the undeleted messages, and these messages
are deleted in class destructor or when .Clear() is called.)
And so for those pending messages (not processed), there is the .Clear call
in WinMain (or dialog-box parent) code.
The disadvantage of this method is that it introduces an external dependency
from the MessageMemoryManager instance, but the good thing is that I'm sure
that I'll have no memory leak (I did some tests, and they were OK), and I
don't need WFSO and flushing the message pump just to delete pending
messages.
Maybe for you these kind of things are very simple and trivial... but I
confess I've learnt a lot :)
Thanks,
Giovanni