Ben
Ben

Reputation: 556

Looking for an alternative to the WaitForInputIdle function, available at any time

I'm working on an automation program, and the WaitForInputIdle function helps me determine when the window of the target app is done initializing. The problem is that, in my case, it works only with the first window - that's how WaitForInputIdle works, only once.

Could the functionality of WaitForInputIdle be implemented in a different way, such that it could be called every time the target process is busy, and wait until it's done?

I thought about posting a dummy message, but I don't think there's a way to find out when it gets removed from the queue.

Edit: I came up with something, an ugly and hacky solution that seems to work:

RECT rc;

if(!GetUpdateRect(hWnd, &rc, FALSE))
{
    rc.left = rc.top = 0;
    rc.right = rc.bottom = 1;
    InvalidateRect(hWnd, &rc, FALSE);
}

do {
    Sleep(100);
} while(GetUpdateRect(hWnd, &rc, FALSE));

I really hope there's something better than that.

Upvotes: 3

Views: 1294

Answers (1)

Ben
Ben

Reputation: 556

Edit: sending WM_NULL, as suggested by Raymond Chen, works for me.

It appears that the PrintWindow hack has no advantages, as internally it just sends the WM_PAINT message.

Old message: I came up with a solution that solves my problem, still a hack but not as ugly.

The idea is misusing the PrintWindow function, which basically posts sends a WM_PAINT message and waits for the window to process it - exactly what I need. Below is the code with some informative comments.

It was tested on Windows XP and Windows 8 and it works as expected, i.e. doesn't fail despite the NULL HDC value.

// BEWARE: HACK BELOW
// PrintWindow is misused here as a synchronization tool
// When calling it, the system sends WM_PAINT and waits for it to be processed
// Note: if hWnd is hung, the following call will hang as well
PrintWindow(hWnd, NULL, 0);

Upvotes: 1

Related Questions