Reputation: 2525
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?
Upvotes: 4
Views: 25009
Reputation: 172270
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();
Upvotes: 26
Reputation: 2880
what you are looking for is the WaitForExit() function.
A Quick google will bring you to http://msdn.microsoft.com/en-us/library/ty0d8k56.aspx
or better still the Exited event that everyone else has mentioned ;)
Upvotes: 1
Reputation: 511
You can attach a handler to the Exited event on the Process object. Here's a link to the MSDN article on the event handler.
Upvotes: 3
Reputation: 124696
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
}
Upvotes: 4