Squall
Squall

Reputation: 4472

Wait for a window message for a defined time

I am programming with winapi. How do I wait for a message for a defined time? If there is no message, I can do another task. I can not use Sleep, because the window callback procedure will be delayed.

while (true){
    ...//wait a message for 30 miliseconds
    GetMessage(&message, hwnd, 0, 0) ) or PeekMessage(&message, hwnd, 0, 0, PM_REMOVE)
    ...
    if ( no_message ){
        call_a_function();
    } else {
        if (finish)
            break;
        TranslateMessage(&message);
        DispatchMessage(&message);
        ...//set to wait 30 minus elapsed time 
    }
}

Upvotes: 2

Views: 1781

Answers (3)

David Heffernan
David Heffernan

Reputation: 613013

This seems like an odd request. Windows programs should be responsive. If call_a_function takes any serious amount of time then your app will get the "This Windows is not responding" treatment.

Also, GetMessage goes into a wait state if there are no posted messages in the queue. I don't understand why your message loop looks so different from the canonical message loop.

I think to answer this question well it would be helpful to know the purpose of your call_a_function routine.

Upvotes: 0

Leo Davidson
Leo Davidson

Reputation: 6143

This is exactly what MsgWaitForMultipleObjects is for.

Upvotes: 5

karlphillip
karlphillip

Reputation: 93410

You could use a timer.

This site also shows how to use a win32 timer.

Upvotes: 1

Related Questions