I have following code:
private void fileSystemWatcher_Changed(object sender, System.IO.FileSystemEventArgs e)
{
System.Diagnostics.Process execute = new System.Diagnostics.Process();
execute.StartInfo.FileName = e.FullPath;
execute.Start();
//Process now started, detect exit here
}
The FileSystemWatcher is watching a folder where .exe files are getting saved into. The files which got saved into that folder are executed correctly. But when the opened exe closes another function should be triggered.
Is there a simple way to do that?
Attach to the Process.Exited event. Example:
System.Diagnostics.Process execute = new System.Diagnostics.Process();
execute.StartInfo.FileName = e.FullPath;
execute.EnableRaisingEvents = true;
execute.Exited += (sender, e) => {
Debug.WriteLine("Process exited with exit code " + execute.ExitCode.ToString());
}
execute.Start();
Process.WaitForExit.
And incidentally, since Process
implements IDisposable
, you really want:
using (System.Diagnostics.Process execute = new System.Diagnostics.Process())
{
execute.StartInfo.FileName = e.FullPath;
execute.Start();
//Process now started, detect exit here
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With