2014-09-02 19 views
1

我正試圖在隱藏模式下使用process.start打開簡單的.net exe/notepad.exe。並且稍後需要進程句柄來使application.exe在一段時間後可見。System.Diagnostics.Process.Start with WindowStyle.Hidden不會重新執行句柄

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

回答

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.

我認爲你唯一的選擇。將正常啓動,獲取句柄,然後將它隱藏起來。
這可能會導致一些閃爍,但它應該工作。爲了減輕閃爍,你可以開始它最小化...

+0

是的,這將有所幫助。是否有任何選項可以處理隱藏進程? – anand 2014-09-02 18:05:36

+0

@anand編輯我的答案。 – 2014-09-02 18:14:18

+0

謝謝。我的實際需求是打開團隊開發人員開發的exe文件,獲取句柄並在.net winform中顯示exe文件。最小化選項不適用於該exe文件。但它適用於winform.exe,記事本。 – anand 2014-09-02 18:25:41