1

I have written a small programme to perform a quick configuration on a client machine and it needs to be able to run with a GUI and silently from the command line. If I run it with the GUI then it works perfectly, if however I try to run it without then it just hangs.

I have traced the problem to this section of code:

    string arg = "/C:\"setup.exe /qn ADD_OPINSIGHTS_WORKSPACE=1 OPINSIGHTS_WORKSPACE_ID=" + workSpaceID + " OPINSIGHTS_WORKSPACE_KEY=" + workSpaceKey + " AcceptEndUserLicenseAgreement=1\"";
log.Info(arg);

// Use ProcessStartInfo class
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "MMASetup-AMD64.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = arg;

try
{
    log.Info("try entered");
    // Start the process with the info we specified.
    // Call WaitForExit and then the using statement will close.
    using (Process exeProcess = Process.Start(startInfo))
    {
        log.Info("Install started");
        exeProcess.WaitForExit(30000);
        log.Info("Install exit code: " + (exeProcess.ExitCode).ToString());
        return (exeProcess.ExitCode).ToString();
    }
}
catch (Exception e)
{
    log.Error("MMA install threw an error: ", e);
    return e.Message;
}

This method is in a seperate class to the GUI and silent code and is run in exactly the same way yet only reaches "Install started" when run silently. I know that the exe does finish so I have tried using the code in this solution but had the same problem: ProcessStartInfo hanging on "WaitForExit"? Why?

Community
  • 1
  • 1
Exitialis
  • 411
  • 6
  • 26

2 Answers2

2

I had the same Problem.

I made a startup class:

  public partial class Startup {

    // WPF App
    private App _app;

    [STAThread]
    public static void Main(string[] args) {
      try {
        //Do what you need
        //Check the args
        //Start your setup silent

        //start the WPF App if need it
        this._app = new App();
        this._app.InitializeComponent();
        this._app.Run();
      } catch (Exception ex) {
        //Logging ex
      }
    }

After that you must change your Application Startup Object to the Startup Class.

1

I was running all of my work asynchronously and because I was not loading the GUI thread Windows was treating the application like a console app. Whereas a GUI thread would call other asynchronous methods and wait for them to finish a console application calls the methods and then closes because it has nothing left to do. The solution was to explicitly make the main thread wait like this:

public static void Main(string[] args)
    {
        try
        {
            Install().Wait();

        }
        catch (Exception ex)
        {

        }
    }

    private static async Task Install()
    {}
Exitialis
  • 411
  • 6
  • 26