使用 Process.Start() 启动后等待 WPF 应用程序加载

发布于 2024-09-09 02:38:54 字数 445 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回眸一遍 2024-09-16 02:38:54

process.Refresh(); 添加到 while 循环。

Add process.Refresh(); to the while loop.

浮光之海 2024-09-16 02:38:54

WaitForInputIdle 使用 while 循环是没有意义的,因为此调用会阻塞当前线程,直到其他进程完成其初始化。之后,它总是立即返回。请阅读帖子 WaitForInputIdle 实际上应该被称为 WaitForProcessStartupComplete –老新事物

正如 raymond 所说,它实际上应该被称为 WaitForProcessStartupComplete

你应该使用这段代码:

if (!wpfProcess.WaitForInputIdle(10000)) // 10 s timout
  throw new ApplicationException("Process takes too much time to start");
_hWnd = net4ReconProcess.MainWindowHandle;

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 Thing

As raymond says it, it should really be called WaitForProcessStartupComplete.

You should use this code:

if (!wpfProcess.WaitForInputIdle(10000)) // 10 s timout
  throw new ApplicationException("Process takes too much time to start");
_hWnd = net4ReconProcess.MainWindowHandle;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文