2015-09-02 57 views
0

代碼看起來很長,但它是一個簡單的程序。
我已經構建了一個控制檯應用程序(TakeScreenshots),將採取從Firefox的網站截圖,鉻&即按順序&將它們保存在一個文件夾中。當我手動運行TakeScreenshots.exe時,所有3個屏幕截圖都會保存。如何等待遠程exe完成 - c#

現在,我已經構建了另一個控制檯應用程序(MyApp),它將執行TakeScreenshots.exe。但這樣,只有firefox截圖被保存,而不是其他的2.沒有例外。它只是說「過程完成」。我想,MyApp不會等待TakeScreenshots完成。

我該如何解決這個問題。

[TakeScreenshots稍後將被放置在幾個遠程計算機&由MyApp的運行]

TakeScreenshots代碼:

private static string[] WebDriversList = ["firefox","chrome","internetexplorer"]; 

private static void TakeAPic() 
{ 
    string url = "http://www.google.com"; 
    string fileNamePrefix = "Test"; 
    string snapSavePath = "D:\\Pics\\"; 

    foreach (string wd in WebDriversList) 
    { 
    IWebDriver NewDriver = null; 
    switch (wd.ToLower()) 
    { 
     case "firefox": 
      using (NewDriver = new FirefoxDriver()) 
      { 
      if (NewDriver != null) 
       { 
       CaptureScreenshot(NewDriver, url, fileNamePrefix, snapSavePath); 
       } 
      } 
      break; 
     case "chrome": 
      using (NewDriver = new ChromeDriver(WebDriversPath)) 
      { 
       if (NewDriver != null) 
       { 
       CaptureScreenshot(NewDriver, url, fileNamePrefix, snapSavePath); 
       } 
      } 
      break; 
     case "internetexplorer": 
      using (NewDriver = new InternetExplorerDriver(WebDriversPath)) 
      { 
      if (NewDriver != null) 
       { 
       CaptureScreenshot(NewDriver, url, fileNamePrefix, snapSavePath); 
       } 
      } 
      break; 
     } 
     if (NewDriver != null) 
     { 
     NewDriver.Quit(); 
     } 
    } 
    } 

private static void CaptureScreenshot(IWebDriver driver,string url,string fileNamePrefix, 
      string snapSavePath) 
{ 
    driver.Navigate().GoToUrl(url); 
    Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot(); 
       ICapabilities capabilities = ((RemoteWebDriver)driver).Capabilities; 
    ss.SaveAsFile(snapSavePath + fileNamePrefix + "_" + capabilities.BrowserName + ".png", 
       ImageFormat.Png);    
} 

MyApp的代碼:

static void Main(string[] args) 
{ 
    ExecuteTakeScreenshot(); 
    Console.WriteLine("PROCESS COMPLETE"); 
    Console.ReadKey(); 
} 

private static void ExecuteTakeScreenshot() 
{ 
    ProcessStartInfo Psi = new ProcessStartInfo("D:\\PsTools\\"); 
    Psi.FileName = "D:\\PsTools\\PsExec.exe"; 
    Psi.Arguments = "/C \\DESK101 D:\\Release\\TakeScreenshots.exe"; 
    Psi.UseShellExecute = false; 
    Psi.RedirectStandardOutput = true; 
    Psi.RedirectStandardInput = true; 
    Process.Start(Psi).WaitForExit(); 
} 

更新: 這是我的錯誤。最初WebDriversPath被分配了「WebDrivers /」。當我將其更改爲實際路徑「D:\ WebDrivers \」時,它工作正常。但我仍然不明白它是如何工作時,手動運行TakeScreenshots.exe它並不從其他控制檯運行

+0

有關調試自己的代碼是什麼? –

+1

爲什麼使用'@''字符串語法並將雙斜線\\放在目錄中?刪除'@'或寫入'D:\ Release \ TakeScreenshots.exe'。 – i486

+0

請提供[mcve](http:// stackoverflow。 com/help/mcve)在這個過程中,它會幫助你調試你自己的問題,如果你創建了一個並且仍然無法解決它......回過頭來發布你的mcve,你試過了什麼,結果是什麼,然後我們會做好準備,幫助你 – JeffC

回答

1

在類似的問題,我已經成功地等待輸入空閒第一。像這樣:

Process process = Process.Start(Psi); 
process.WaitForInputIdle(); 
process.WaitForExit(); 

你可以試試這個。對我而言,需要使用Adobe Reader打印PDF,而不是在之後的早期關閉它。

例子:

Process process = new Process(); 
process.StartInfo.FileName = DestinationFile; 
process.StartInfo.Verb = "print"; 
process.Start(); 
// In case of Adobe Reader the following statement is needed: 
process.WaitForInputIdle(); 

process.WaitForExit(2000); 
process.WaitForInputIdle(); 
process.Kill(); 
+0

在調試時,我在WaitForInputIdle得到這個異常'因爲進程(5600)已經退出無法處理請求' – Qwerty

+0

這聽起來像你的應用程序正在退出非常快。它不會執行任何操作。嘗試並從TakeScreenshots.exe中記錄文件,並確定該程序是否對其他瀏覽器執行的操作不是firefox。 –