我试图在隐藏模式下使用process.start打开简单的.net exe / notepad.exe。我需要稍后使用进程句柄使application.exe在一段时间后显示它。
只能在WindowStyle.Minimized,WindowStyle.Maximized,WindowStyle.Normal中获取句柄。在隐藏的风格中,它总是给我0。
如何在不使用Thread.Sleep的情况下获取句柄。它需要我们等待几秒钟才能得到处理。根据其性能(大量数据),某些exe需要更多的等待时间。
public static void LaunchExe()
{
var proc = new Process
{
StartInfo =
{
FileName = "Notepad.exe", //or any simple .net exe
WindowStyle = ProcessWindowStyle.Hidden
}
};
proc.Start();
proc.WaitForInputIdle(800); //is it possible to avoid this.
Thread.Sleep(3000); //is it possible to avoid this.
Console.WriteLine("handle {0}", proc.MainWindowHandle);
//ShowWindowAsync(proc.MainWindowHandle, 1); //planned to use, to make it visible.
}
答案 0 :(得分:0)
您可以这样做:
IntPtr ptr = IntPtr.Zero;
while ((ptr = proc.MainWindowHandle) == IntPtr.Zero)
{ proc.WaitForInputIdle(1 * 60000); Thread.Sleep(10); } // (1*60000 = 1min, will give up after 1min)
这样你就不会再浪费时间了。
您无法获得隐藏进程的句柄。
According to MS: A process has a main window associated with it only if the process has a graphical interface. If the associated process does not have a main window, the MainWindowHandle value is zero. The value is also zero for processes that have been hidden, that is, processes that are not visible in the taskbar.
我认为你唯一的选择是正常启动,获取句柄,然后将其隐藏起来 这可能会导致一些闪烁,但它应该工作。为了减轻闪烁,你可以把它开始最小化......