views:

60

answers:

3

I have a program that spawns another process from within one of the loaded assemblies. This second process goes and grabs an installer for the program that spawned it (confused yet?). This second process just starts the installer at a specific web address:

System.Diagnostics.Process.Start(www.mySite.com/myInstaller.exe);

The problem is that if this installer is run before the parent program's assemblies have been unloaded it will not install correctly as a couple of the program's assemblies need to be overwritten as part of the install.

We need this process to be called from our parent program.

My question is how do I spawn this process so that it runs only after the assemblies have unloaded? Is there a better way to do all this?

+3  A: 

Check in the child process whether the parent process has exited.

Assuming that you know the name of your application this would be a simple check:

Process[] processes = Process.GetProcessesByName("calc");
Array.ForEach(processes, p => p.WaitForExit());
0xA3
+1  A: 

So basically, you have an application that "self-updates". Your main app calls an updater, which goes and finds the installer and runs that. The installer then runs, and could easily replace the assembly that called the updater, and the updater. When the installer's done, you restart the application.

You can (and .NET does by default) use the OS shell to start the new process. This process will have no connection to its parent; if, for instance, you created an executable whose sole purpose was to open Notepad, your executable would call Process.Start("notepad.exe"), Notepad would open, and your process would close, leaving Notepad open.

To make sure your application has closed, you can use Process.GetProcesses() in the installer, which will get the list of processes you'd see by using the Task Manager. Scan the list for your application's executable name (and if applicable, the updater); if it exists, try sleeping for a second, then try again. Still there? Alert the user to manually close the application, then when they say they have, check again.

KeithS
A: 

If your installer is "dumb" make another app purely to launch the installer once the Main program is fully exited.

Though having said that, I have an auto updated in many of my bits of software and have never had this problem.

Keith Nicholas