views:

157

answers:

1

Is it possible to prioritize a message sent with PostMessage (or any of the other related methods)?

IIRC, the WM_PAINT message, for instance, is only processed when there are no other messages in the queue. Is it possible to achieve a similiar behavior with custom messages?

If I use WM_PAINT with special parameters in order to deliver a custom message to a window (of which I control the WndProc) will it have a similiar behavior?

A: 

For WM_PAINT the windowing code in DefWndProc just sets a flag and then checks that flag only if the queue is empty the next time GetMessage is called. Some mouse messages are also coalesced (older ones are removed when the newer ones arrive).

The real answer depends on the behaviour you're actually wanting to achieve.

If you are trying to avoid reentrancy just check a flag for a quick exit, something like:

////bool processing = false; // class/window instance variable
...
void HandleCustomMessage()
{
    ////if (processing)
    ////{
    ////    return;
    ////}

    ////processing = true;
    DoSomething();
    ////processing = false;
}

If you want an actual priority queue, there are numerous PQ implementations. Add the data item to the PQ and then post a custom message (always the same ID). The custom message handler then asks the PQ for the highest priority item.


Another option is to intercept the GetMessage loop, use a call to PeekMessage to see if there is anything to do, then call GetMessage if a message is available, or check your PQ otherwise. You don't need a custom message with this approach.

devstuff
Not sure I understand the flag approach. UI queues are single-threaded so you'll never hit the condition anyway (Or if you are referring to avoiding reentrancy across multiple queues, I don't see how it's relevant to prioritizing). Using a custom message + seperate PQ is a good idea, altough the prioritizing won't work together with other "non-custom" messages. I was wondering wether there is some internal mechanism for that?
sold
Oops, half asleep and forgot about the single UI thread - amazing how quickly you forget when you're not dealing with this stuff every day.
devstuff