【问题标题】:System.Diagnostics.Process ExitCode 1System.Diagnostics.Process ExitCode 1
【发布时间】:2019-01-15 17:56:30
【问题描述】:

我正在使用简单的 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进程的进程状态?

谢谢

【问题讨论】:

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


    【解决方案1】:

    我终于实现了这个解决方案,我不满意,但它确实有效。 在打开新的 uri 后,使用 -nomerge 参数,我们等待三秒钟,最后搜索拥有这个新页面的新进程。 现在我们调用 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;
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-16
      • 2018-05-26
      • 2013-08-28
      • 2011-02-05
      • 1970-01-01
      • 2012-07-27
      • 2011-01-12
      相关资源
      最近更新 更多