检测单个Windows进程退出事件C#

本文关键字:出事件 退出 进程 单个 Windows 检测 | 更新日期: 2023-09-27 18:20:14

好吧,我又有了一个,现在我正在开发一个按钮,代码中称为installbtn。

现在我的代码里有。

private void installbtn_Click(object sender, EventArgs e)
        {
// Access the internal exe resource pcc - the pcc is Path Copy Copy, which i am using as a
// requirement to use this software
   byte[] pccFile = Properties.Resources.pcc;  
// The resource pcc.exe as a binary called pcc which is then used as a byte called pccFile  
   string pccExe = Path.Combine(Path.GetTempPath(), "pcc.exe");  
// The Executable and its filename + Extenstion
   using (FileStream exeFile = new FileStream(pccExe, FileMode.Create))
   exeFile.Write(pccFile, 0, pccFile.Length);
// Write the file to users temp dir
   Process.Start(pccExe);
// Start the Installer
   installbtn.Text = "Installing...";
   StatusLabel1.Text = "Installing PCC Now...";
// Indicate on the form, the current process status.

// Here i want the application to check if pccExe has closed
// after the user has installed the component and its process "pcc.exe" exits
   MessageBox.Show("Module Installed /r/nPlease Start the Application", "Application Module Installed");
   installbtn.Text = "Restart Now!";
   StatusLabel1.Text = "Please Restart the Application";
// and if it has then show a message box and reflect in the form
// i want it to quit and restart the application after the message box is closed
}

现在,当安装程序完成时,我希望能够检测安装程序在安装后何时关闭("pcc.exe"),然后在应用程序关闭后重新加载。不确定是否可能,但我很感激你的帮助。

谢谢肖恩。

检测单个Windows进程退出事件C#

您可以使用Process.WWaitForExit()

WaitForExit()重载用于使当前线程等待直到相关联的过程终止。此方法指示进程组件等待进程的时间无限长和事件处理程序退出

Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
process.StartInfo = startInfo;
startInfo.FileName = exeToRun;    
process.Start();
process.WaitForExit();

Start返回一个Process对象,您可以在该对象上等待(或启动一个等待的线程等)。