PostMessage() Problem

Posted on 2006-06-08
Last Modified: 2013-12-03
I have a window with a listbox that displays some progress messages from a thread that is running.  The thread basically formats the text into a buffer and then calls PostMessage().  Most of the time this works ok but sometimes the text gets garbled.

In reading the MSDN I located the following:

If you send a message in the range below WM_USER to the asynchronous message functions (PostMessage, SendNotifyMessage, and SendMessageCallback), its message parameters cannot include pointers. Otherwise, the operation will fail. The functions will return before the receiving thread has had a chance to process the message and the sender will free the memory before it is used.

I suspect this is my issue.

Question:  what's the best way to avoid this problem?  My thread has no clue when Windows processes the message and grabs the stuff in the buffer.  I can't use SendMessage because the thread can't wait around all day.

My thought is that I need to implement some sort of a queue in another thread that will accept these calls to output text to the window asynchronously, store the text in the queue, call SendMesasge, then remove the text from the queue.  But that seems (to me at least) to be an overly complicated solution to what I think should be a fairly common problem.

Thanks in advance...
Question by:jhance
    LVL 86

    Expert Comment

    Stupid idea - why not using 'GlobalAddAtom()' for that purpose and post the atom via the message parameters? You can delete the atom after processing. A more universal way could indeed be e.g. a std::queue<string> secured by a CRITICAL_SECTION that sync's the access.
    LVL 32

    Author Comment


    Thanks...  But I'm unfamiliar with GlobalAddAtom().  I see what it says in the MSDN docs but I'm unclear how this would work in solving the problem above.  Specially, how would using this help.  And how would you delete it (or WHO would delete it after "processing"?

    Maybe a quick sample?
    LVL 86

    Expert Comment

    I haven't used that since Win 3.1, so I am not really sure how that works now, but the idea would be

    //in the thread

    LPSTR test = "test";

    ATOM a = GlobalAddAtom(test);

    The problem probably is that you cannot send that directly to the listbox but will have to translate it using 'GlobalGetAtomName()', which leaves you with the same problem, except if you can use a 'SendMessage()' in the receiving WindowProc... thus said, I am starting to wonder whether this idea is even stupider than I thougt.
    LVL 32

    Author Comment

    I think it solves one problem but creates another.  

    It will prevent the data buffer of the string to be written from being deleted or overwritten before it's output to the screen but finding and cleaning up all the left over atoms will be another problem.  You couldn't do it right after the PostMessage because you'd be right back where you started, namely the atom could be gone by the time the message loop processed the message that said to output it...

    I'm not averse to implementing a queue/thread to do this, I was just hoping that there was some trick that every Windows programmer but me knew about that solved this...
    LVL 48

    Expert Comment

    >> I can't use SendMessage because the thread can't wait around all day.
    Do you post message to your own program? In this case you must ensure that it always ready to handle messages and doesn't execute long operations (seconds, days...) without handling message queue. This is basic requirement to any program.
    Assuming that message target works properly, you can use SendMessage from a worker thread. To prevent deadlock, it is possible to use SendMessageTimeout.
    LVL 48

    Accepted Solution

    However, classic solution is some string container with critical section. If your data is list of separate messages and not stream, this can be simple list<string>, and not queue. Thread adds new string to this list and posts message to main thread. Main thread reads all strings from the list and clears it. If list is empty, main thread does nothing - pretty simple.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    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.

    Suggested Solutions

    Title # Comments Views Activity
    link access with Quickbooks 9 32
    SSIS questions 4 77
    Problem to adjust sheet 1 78
    GUI: DIalog Stacking and Popping in MS C++ 4 49
    This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (, we saw how to put the Delete button back there where it belongs.  "Delete" is …
    After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    761 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

    8 Experts available now in Live!

    Get 1:1 Help Now