Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: How do I run a batch process after a program exits (not called during it)

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?

like image 229
myermian Avatar asked Dec 28 '22 08:12

myermian


1 Answers

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 */
}
like image 196
P Daddy Avatar answered Jan 10 '23 03:01

P Daddy