Skip to main content
 首页 » 编程设计

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

2024年02月06日13pengyingh

好吧,我有另一个,现在我正在处理一个按钮,在代码中称为 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"),然后在关闭后重新加载应用程序。不确定是否可行,但我将不胜感激。

谢谢肖恩。

请您参考如下方法:

你可以使用 Process.WaitForExit()

The WaitForExit() overload is used to make the current thread wait until the associated process terminates. This method instructs the Process component to wait an infinite amount of time for the process and event handlers to exit

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