-1

i'm using a simple C# static method in order to launch windows application. All works fine with all 32 or 64 bit application except for iexplore.exe.

When i call:

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

System.Diagnostics.Process WaitForExit() method does not wait for iexplore.exe closure and ExitCode get back exitcode=1.

Here is 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;
}

I've done a lot of search and the only workaround founded is to add -nomerge keyword on System.Diagnostic.Process.ProcessStartInfo Arguments property. WaitForExit() works fine on other windows .exe process but NOT with iexplore.exe process. How could we test the process status of iexplore.exe process?

Thank you

Fabio Borghi
  • 23
  • 1
  • 6

1 Answers1

0

Finally i've implemented this solution, i'm not satisfied but it's works. After opening new uri, with -nomerge argument, we wait for three secs and finally search for the new process that own this new page. Now we call WaitForExit() that works as expected, here is my code, any suggestions are welcome:

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;
}
}
Fabio Borghi
  • 23
  • 1
  • 6