How is the .NET event, Application.OnIdle, coded in Win32?
How is the .NET event, Application.OnIdle, coded in Win32? (i.e. What is going on beh开发者_开发百科ind the scenes?).
A classic Win32 message loop is something like this:
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
There is also a PeekMessage()
function that tests to see whether a message is available. With that, you can modify the message loop as:
while (GetMessage(&msg, NULL, 0, 0)) {
do {
TranslateMessage(&msg);
DispatchMessage(&msg);
} while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE));
onIdle();
}
The above loop will call the translate/dispatch while there are still messages available to process, then when there are no more it will call onIdle()
. Then it returns to the outer loop to call GetMessage()
again to wait for the next message.
Note that this is one possible way to implement onIdle functionality in Win32. I don't know whether this is the same way .NET does it.
精彩评论