System.Diagnostics.Process ExitCode 1

时间:2018-08-08 12:47:55

标签: c# process exit-code system.diagnostics waitforexit

我正在使用简单的C#静态方法来启动Windows应用程序。 除iexplore.exe之外,所有功能均可在所有32位或64位应用程序上正常运行。

当我打电话时:

 ExecHttp(@"C:\Program Files (x86)\Internet Explorer\iexplore.exe", "http://www.google.it");

System.Diagnostics.Process WaitForExit()方法不会等待iexplore.exe关闭,并且ExitCode会返回exitcode = 1。

这是ExecHttp:

public static int ExecHttp(String strURL, String strArguments)
{
    int intExitCode = 99;
    try
    {
        Process objProcess = new System.Diagnostics.Process();
        strArguments = "-nomerge " + strArguments;
        System.Diagnostics.ProcessStartInfo psi = new ProcessStartInfo(strURL, strArguments);
        psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
        psi.UseShellExecute = true;
        objProcess.StartInfo = psi;
        objProcess.Start();
        Thread.Sleep(2000);
        objProcess.WaitForExit();
        intExitCode = objProcess.ExitCode;
        objProcess.Close();
    }
    catch (Exception ex)
    {
        //log the exception 
        throw;
    }
    return intExitCode;
}

我已经做了很多搜索,发现的唯一解决方法是在System.Diagnostic.Process.ProcessStartInfo Arguments属性上添加-nomerge关键字。 WaitForExit()在其他Windows .exe进程上工作正常,但不适用于iexplore.exe进程。 我们如何才能测试iexplore.exe进程的进程状态?

谢谢

1 个答案:

答案 0 :(得分:0)

最后,我已经实现了此解决方案,我不满意,但是它可以工作。 使用-nomerge参数打开新uri后,我们等待三秒钟,最后搜索拥有该新页面的新进程。 现在我们调用可以正常工作的WaitForExit(),这是我的代码,欢迎提出任何建议:

public static int ExecHttp(String strBrowserApp, String strURL, String strSessionName, ref String strMessage)
{
int intExitCode = 99;
try
{
     strMessage = String.Empty;
     System.Diagnostics.Process.Start(strBrowserApp, "-nomerge " + (String.IsNullOrEmpty(strURL) ? "" : strURL));
     System.Threading.Thread.Sleep(3000);
     System.Diagnostics.Process objProcess = null;
     System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcesses();
     foreach (System.Diagnostics.Process proc in procs.OrderBy(fn => fn.ProcessName))
     {
          if (!String.IsNullOrEmpty(proc.MainWindowTitle) && proc.MainWindowTitle.StartsWith(strSessionName))
          {
               objProcess = proc;
               break;
          }
      }
      if (objProcess != null)
      {
          objProcess.WaitForExit();
          intExitCode = 0;
          objProcess.Close();
      }
}
catch (Exception ex)
{
     strMessage = ex.Message;
}
}
相关问题