C#process.start,我怎么知道这个过程是否结束了?

Kel*_*ari 23 c# process

在C#中,我可以开始一个进程

的Process.Start(的Program.exe);

如何判断程序是否仍在运行,或者程序是否已关闭?

Aus*_*nen 49

MSDN System.Diagnostics.Process

如果您想现在就知道,可以查看HasExited酒店.

var isRunning = !process.HasExited;
Run Code Online (Sandbox Code Playgroud)

如果这是一个快速的过程,只需等待它.

process.WaitForExit();
Run Code Online (Sandbox Code Playgroud)

如果您在后台启动,请在将EnableRaisingEvents设置为true后订阅Exited事件.

process.EnableRaisingEvents = true;
process.Exited += (sender, e) => {  /* do whatever */ };
Run Code Online (Sandbox Code Playgroud)


coo*_*ine 12

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");
}
Run Code Online (Sandbox Code Playgroud)