English 中文(简体)
PostMessage with priority?
原标题:

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?

问题回答

I wanted a background thread to notify the main that that results where available to displayed to the user. But if you simply post a message to the main thread, it will get processed immediately.

This lead to the application not responding to paint or user input messages, because my posted message always took priority.

The trick i used was the knowledge that WM_TIMER messages have lower priority than WM_PAINT messages.

So rather than posting a message to the main form, I would set a timer.

Timer messages have lower priority than

  • other posted messages
  • WM_PAINT messages

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.




相关问题
How to read exact number of bytes from a stream (tcp) socket?

In winsock, both the sync recv and the async WSARecv complete as soon as there is data available in a stream socket, regardless of the size specified (which is only the upper limit). This means that ...

AcquireCredentialsHandle returns SEC_E_NO_CREDENTIALS

I created a self-signed certificate (created using OpenSSL) and installed it into the Certificate Store using the Certificates MMC snap-in (CertMgr.msc) on Windows Vista Ultimate. I have managed to ...

Calling Win32 EnumThreadWindows() in C#

I m trying to get a call to EnumThreadWindows working, but I always get a Wrong Parameter-Error, although my code is nearly the same as this example on pinvoke.net. I don t know why this doesn t work: ...

COM Basic links

folks can you provide me the tutorial link or .pdf for learning basic COM?. i do google it.. still i recommend answers of stackoverflow so please pass me.. Thanks

Handling multiple windows WIN32 API

HI I m trying to create an application in the Win32 environment containing more than one window. How do i do that? all the Win32 Tutorials on web i found only showed how to manage one window. How do i ...

Creating a thread in DllMain?

It seems that when a thread is created from within DllMain upon DLL_PROCESS_ATTACH it won t begin until all dll s have been loaded. Since I need to make sure the thread runs before I continue, I get a ...

热门标签