Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# process.start, how do I know if the process ended?

Tags:

c#

process

In C#, I can start a process with

process.start(program.exe);

How do I tell if the program is still running, or if it closed?

like image 679
Keltari Avatar asked Sep 05 '12 02:09

Keltari


2 Answers

MSDN System.Diagnostics.Process

If you want to know right now, you can check the HasExited property.

var isRunning = !process.HasExited; 

If it's a quick process, just wait for it.

process.WaitForExit(); 

If you're starting one up in the background, subscribe to the Exited event after setting EnableRaisingEvents to true.

process.EnableRaisingEvents = true; process.Exited += (sender, e) => {  /* do whatever */ }; 
like image 71
Austin Salonen Avatar answered Sep 27 '22 16:09

Austin Salonen


Process p = new Process(); p.Exited += new EventHandler(p_Exited); p.StartInfo.FileName = @"path to file"; p.EnableRaisingEvents = true; p.Start();  void p_Exited(object sender, EventArgs e) {     MessageBox.Show("Process exited"); } 
like image 29
coolmine Avatar answered Sep 27 '22 15:09

coolmine