简体   繁体   English

如何等待远程exe完成-C#

[英]How to wait for the remote exe to complete - c#

The code looks lengthy but it's a simple program. 该代码看起来很冗长,但这是一个简单的程序。
I have built a console app (TakeScreenshots) that will take website screenshots from firefox, chrome & ie in that order & save them in a folder. 我已经构建了一个控制台应用程序(TakeScreenshots),它将从firefox,chrome和IE中获取网站屏幕截图,并将其保存在文件夹中。 When I manually run TakeScreenshots.exe, all 3 screenshots are saved. 当我手动运行TakeScreenshots.exe时,将保存所有3个屏幕截图。

Now, I have built another console app (MyApp) that will execute TakeScreenshots.exe. 现在,我构建了另一个控制台应用程序(MyApp),它将执行TakeScreenshots.exe。 But in this way, only the firefox screenshot is saved and not of the other 2. There are no exceptions. 但是通过这种方式,只保存了firefox屏幕截图,而不保存其他屏幕截图2。也不例外。 It just says "Process Complete". 它只是说“处理完成”。 I guess, MyApp is not waiting for the TakeScreenshots to complete. 我想,MyApp不会等待TakeScreenshots完成。

How can I fix this. 我怎样才能解决这个问题。

[TakeScreenshots will later be placed in few remote computers & run by MyApp] [TakeScreenshots稍后将放置在几台远程计算机中,并由MyApp运行]

TakeScreenshots code: 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 code: 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();
 }

Update: It was my mistake. 更新:这是我的错误。 Initially WebDriversPath was assigned "WebDrivers/". 最初, WebDriversPath被分配为“ WebDrivers /”。 When I changed it to the actual path "D:\\WebDrivers\\", it worked. 当我将其更改为实际路径“ D:\\ WebDrivers \\”时,它起作用了。 But I still dont understand how it worked when TakeScreenshots.exe was run manually and it doesn't when run from another console 但是我仍然不了解手动运行TakeScreenshots.exe时它是如何工作的,而从另一个控制台运行时却不行。

In similar problems I have had success with waiting for input idle first. 在类似的问题中,我已经成功地等待输入空闲。 Like this: 像这样:

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

You could try this. 你可以试试看。 For me it was needed to print a pdf using Adobe Reader and not close it to early afterwards. 对我来说,需要使用Adobe Reader打印pdf,并且不要在早期关闭它。

Example: 例:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM