【问题标题】:Detect Single Windows Process Exit Event C#检测单个 Windows 进程退出事件 C#
【发布时间】:2014-09-01 09:15:47
【问题描述】:

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

谢谢肖恩。

【问题讨论】:

标签: c# process exe


【解决方案1】:

你可以使用Process.WaitForExit()

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

Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
process.StartInfo = startInfo;

startInfo.FileName = exeToRun;    
process.Start();
process.WaitForExit();

【讨论】:

  • 我已经放入了一个 try catch 块,它给了我消息,没有与对象关联的进程。我正在尝试从 64 位机器定位 win32 进程。之后,它继续正常运行其余代码。因为发生的事情是,当它启动存储在字节中的内部 exe 时,它​​正在启动,但是它需要等到程序和进程正常关闭。我可以提供源代码,因为它是免费的即时制作。
  • @Shaun'Shorty'Cassidy 只是对代码做了一个小改动,在调用 process.WaitForExit() 之前调用 process.Start()
  • 是的,成功了,现在一切都好,感谢 Kurubaran 确保在我的源代码中感谢您。
  • @Shaun'Shorty'Cassidy 很高兴它帮助了你。
【解决方案2】:

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

【讨论】:

  • 你能给我举个例子吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多