Thursday, April 28, 2011

Closing application and running an external application

I am writing the update system for my application and I need to shutdown the application to overwrite the exe but after it shuts down I need to run the update executable, how can I do this?

From stackoverflow
  • Could you just start the updater using Process.Start and have it wait until your main program closes? I think that would be the easiest solution.

    Or you could have a separate launcher program that will check for updates and update before launching the main application. But this leads to the same problem if you have to update the launcher.


    Along the lines of this:

    static void Main(string[] args)
    {
      var haveToUpdate = ...;
      if (haveToUpdate)
      {
        Process.Start("update.exe");
        Environment.Exit(0);
      }
    }
    
    static void Main(string[] args)
    {
      var processes = Process.GetProcessesByName("program.exe");
    
      if (processes.Length > 1)
        throw new Exception("More than one program.exe running");
      else if (processes.Length == 0)
        Update();
      else
        processes[0].Exited += new EventHandler(Program_Exited);
    }
    
    static void Program_Exited(object sender, EventArgs e)
    {
      Update();
    }
    
    static void Update()
    {
      // ...
    }
    
    Daniel Brückner : There is a bug in your code. If two processes named "program.exe" are running, the one to be updated exits, and then you are waiting on the wrong process. I would avoid waiting based on the process name. Some kind of communication between the two processes would probably be better.
    Daniel Brückner : Maybe a named semaphore in combination with a short delay. Or just repeatly check if the executable can be opened for writting.
    Samuel : Did you miss the Exception part? Of course you would want more robust code, but this is a concept only.
    Daniel Brückner : No, I didn't miss this case. And of course it is just an example. My point was the following. 2 processes "program.exe" running (one trying to updated and another) => start "update.exe" => exit => now only one process "program.exe" remaining => "update.exe" is going to wait on a unreleated process.
    Samuel : Well either you have to limit the main program to one instance or it would get really complicated with all the race conditions for running two or more and updating them.
  • Run the update executable before you shutdown the application.

  • I guess you need another small update application. You start it with Process.Start(), let this helper process replace the executable and start up the new one again with Process.Start().

  • Do it in reverse. Get the entire updated file down using the old application (storing it temporarily) and then start the updater application using process start. All the update has to do is copy the new file over the old file and restart the application.

    The updater can catch the access denied exceptions and wait for the application to become available for being copyied over. Once the copy is done - it deletes the temporary new file and starts the application again before shutting down.

    Then you can update the updater (using the normal application) if you need to.

0 comments:

Post a Comment