[英]C# process.start, how do I know if the process ended?
In C#, I can start a process with 在C#中,我可以开始一个进程
process.start(program.exe);
的Process.Start(的Program.exe);
How do I tell if the program is still running, or if it closed? 如何判断程序是否仍在运行,或者程序是否已关闭?
MSDN System.Diagnostics.Process MSDN System.Diagnostics.Process
If you want to know right now , you can check the HasExited
property. 如果您现在想知道,可以查看
HasExited
属性。
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. 如果您在后台启动,请在将EnableRaisingEvents设置为true后订阅Exited事件。
process.EnableRaisingEvents = true;
process.Exited += (sender, e) => { /* do whatever */ };
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");
}
如果使用静态Process.Start()
调用(或使用new
创建实例),请确保保存Process
对象,然后检查HasExited
属性,或者根据需要订阅Exited
事件。
Take a look at the MSDN documentation for the Process class . 查看Process类的MSDN文档 。
In particular there is an event ( Exited ) you can listen to. 特别是有一个你可以听的事件( 退出 )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.