views:

43

answers:

1

How is the .NET event, Application.OnIdle, coded in Win32? (i.e. What is going on behind the scenes?).

A: 

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.

Greg Hewgill
A seemingly plausible solution. Thank Greg. Also found this thread: http://www.eggheadcafe.com/software/aspnet/32039981/win32-equivalent-of-onidle.aspx