簡體   English   中英

System.Diagnostics.Process.Starting與WindowStyle.Hidden不會重新調整句柄

[英]System.Diagnostics.Process.Start with WindowStyle.Hidden doesn't retun handle

我正在嘗試在隱藏模式下使用process.start打開簡單的.net exe / notepad.exe。 並且我需要稍后的進程句柄來使application.exe在一段時間后使其可見。

  1. 只能在WindowStyle.Minimized,WindowStyle.Maximized,WindowStyle.Normal中獲取句柄。 在隱藏樣式中,它始終使我為0。

  2. 如何在不使用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. } 

您可以執行以下操作:

        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)

這樣,您就不會浪費更多的時間。

您無法獲得隱藏進程的句柄。

據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. 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.

我認為您唯一的選擇是正常啟動,獲取手柄,然后將其設置為隱藏。
這可能會引起閃爍,但是應該可以。 為了減輕閃爍,可以將其最小化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM