views:

66

answers:

2

I have the following setup:

MainApp.exe checks for updates, downloads latest updates. The problem is that sometimes the updates are DLLs that the MainApp.exe is currently using. So, I thought maybe just dump all the files into an Update (temp) folder and when the program exits run a batch file that overwrites the DLLs and then relaunches the program. This is common, I've seen it be done (Spybot Search and Destroy for example).

My question is how do you make a program run a process AFTER it exits?

Alternatively, can a batch program be called DURING the program, but wait until AFTER the program is closed to start it's actual batch?

Oh, and I doubt this would be any different in a WPF Application, but in case it is... I'm writing my App as a WPF App.

P.S. I think in Unix something similar is a Forking Exec?

+3  A: 

In the first app, pass the second app your process id:

using System.Diagnostics;

static void Main(){
    /* perform main processing */
    Process.Start("secondapp.exe", Process.GetCurrentProcess().Id.ToString());
}

In the child process, wait for the first to exit:

using System.Diagnostics;

static void Main(string[] args){
    Process.GetProcessById(int.Parse(args[0]).WaitForExit());
    /* perform main processing */
}
P Daddy
You're missing a ) at the end of WaitForExit() :)
Blam
@Blam: Yup. Fixed.
P Daddy
@P Daddy: Thanks. Exactly what I was looking for. :)
myermian
A: 

You could try, initiating the second process from Exit event of the wpf applciation. [App.Xaml.cs]

public partial class App : Application
{
    public App()
    {
        this.Exit += (s, e) =>
            {
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo = new System.Diagnostics.ProcessStartInfo("notepad.exe");
                p.Start();
            };
    }
}
Avatar