Reputation: 349
c# I want to Detect if a launched program has been closed. I am currently launching it with the command
Process.Start(Environment.CurrentDirectory + @"\Card Downloader.exe");
Has anyone got a way of doing this perhaps using a different launcher?
Upvotes: 3
Views: 3315
Reputation: 2797
The Process.Start() method returns a Process object. Assign it to a variable and call WaitForExit() on.
Source: http://msdn.microsoft.com/en-us/library/fb4aw7b8.aspx
Process process = Process.Start("notepad");
process.WaitForExit();
Upvotes: 4
Reputation: 166
You need to subscribe to the process Exited event, but also set the EnableRaisingEvents flag to true.
var process = Process.Start(Environment.CurrentDirectory + @"\Card Downloader.exe");
process.EnableRaisingEvents = true;
process.Exited += (sender, e) =>
{
...
};
If you don't set the flag, the event is raised only if the process is closed during or before the user has performed a call to the HasExited property, according to MSDN.
Upvotes: 4
Reputation: 1288
You can use Process.Exit
event
var myProcess = new Process();
...
myProcess.Exited += new EventHandler(myProcess_Exited);
myProcess.Start();
Upvotes: 2
Reputation: 1039498
The Process.Start
method returns a Process instance. On this instance you could use some of the available methods such as WaitForExit
or subscribe to the Exited
event which will be triggered when this process ends.
var process = Process.Start(Environment.CurrentDirectory + @"\Card Downloader.exe");
process.Exited += (sender, e) =>
{
// this will be called when the process exists
};
Upvotes: 8