使用 Process.Start() 启动后等待 WPF 应用程序加载
我有一个 WinForms 应用程序,它使用 Process.Start 启动运行的 wpf 进程。我想知道 WPF 进程何时完成加载,并且我可以访问 process.MainWindowHandle 属性(在完全加载之前其值为 0)。
我尝试轮询,但句柄始终为 0。但是,如果我调试并等待(在 Process.Start
之后)WPF 应用程序加载,那么我将获得正确的句柄。
不起作用:
int maxCount=100000;
int count=0;
do
{
wpfProcess.WaitForInputIdle();
_hWnd = net4ReconProcess.MainWindowHandle;
count++;
} while (_hWnd.ToInt32() == 0 || count > maxCount);
I have a WinForms app that starts a wpf process running using Process.Start
. I would like to know when the WPF process is finished loading and I can access the process.MainWindowHandle
property (its 0 before its completly loaded).
I tried polling but the handle is always 0. However, if I debug and wait (after Process.Start
) for the WPF app to load - I then will get the correct handle.
Does not work:
int maxCount=100000;
int count=0;
do
{
wpfProcess.WaitForInputIdle();
_hWnd = net4ReconProcess.MainWindowHandle;
count++;
} while (_hWnd.ToInt32() == 0 || count > maxCount);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
process.Refresh();
添加到 while 循环。Add
process.Refresh();
to the while loop.对
WaitForInputIdle
使用 while 循环是没有意义的,因为此调用会阻塞当前线程,直到其他进程完成其初始化。之后,它总是立即返回。请阅读帖子 WaitForInputIdle 实际上应该被称为 WaitForProcessStartupComplete –老新事物正如 raymond 所说,它实际上应该被称为
WaitForProcessStartupComplete
。你应该使用这段代码:
Using a while loop for
WaitForInputIdle
is a non-sense because this call blocks the current thread until the other process has finished its initialization. After that, it always returns immediately. Please read the post WaitForInputIdle should really be called WaitForProcessStartupComplete – The Old New ThingAs raymond says it, it should really be called
WaitForProcessStartupComplete
.You should use this code: