c# MainWindowHandle always zero

允我心安 提交于 2019-12-23 01:12:35

问题


I read a few threads about the MainWindowHandle but i couldnt find a solution for my problem, i'm starting a gui application and want to get the MainWindowHandle through the process object, but the handle value is always zero if i'm not going to wait with thread.sleep() until the gui is loaded. i'm also tried to use WaitForInputIdle but it didnt help at all.

process.Start();

process.WaitForInputIdle(1000);
while (process.MainWindowHandle == IntPtr.Zero)
{
     Thread.Sleep(100);
}
// do something with the handle

he is never leaving the while, if i'm replacing the waitforinputidle with a normal thread.sleep he gets the handle right.

to put it in simply words: i only want to continue with my code if i get a handle != zero but i dont want to wait a static time for this


回答1:


The value stored in MainWindowHandle is cached. Add a process.Refresh() within your loop to invalidate that value:

while (process.MainWindowHandle == IntPtr.Zero)
{
    Thread.Sleep(100);
    process.Refresh();
}


来源:https://stackoverflow.com/questions/10102526/c-sharp-mainwindowhandle-always-zero

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!