-1

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

谢谢

4

1 回答 1

0

最后我实现了这个解决方案,我不满意,但它是有效的。在打开新的 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;
}
}
于 2018-08-11T14:47:58.817 回答