寻找 WaitForInputIdle 函数的替代方法,随时可用 [英] Looking for an alternative to the WaitForInputIdle function, available at any time

查看:24
本文介绍了寻找 WaitForInputIdle 函数的替代方法,随时可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个自动化程序,WaitForInputIdle 函数帮助我确定目标应用程序的窗口何时完成初始化.问题是,就我而言,它仅适用于第一个窗口 - 这就是 WaitForInputIdle 的工作方式,只有一次.

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.

WaitForInputIdle 的功能是否可以以不同的方式实现,这样可以在每次目标进程繁忙时调用它,并等到它完成?

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.

我想出了一个看起来可行的丑陋和骇人听闻的解决方案:

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.

推荐答案

Edit: 发送 WM_NULL,按照 Raymond Chen 的建议,有效对我来说.

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

看起来 PrintWindow hack 没有任何优势,因为它在内部只是发送 WM_PAINT 消息.

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.

这个想法滥用了 PrintWindow 函数,它基本上 posts 发送 WM_PAINT 消息并等待窗口处理它 - 这正是我需要的.下面是带有一些信息性注释的代码.

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.

它在 Windows XP 和 Windows 8 上进行了测试,它按预期工作,即尽管 HDC 值为 NULL 也不会失败.

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);

这篇关于寻找 WaitForInputIdle 函数的替代方法,随时可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆