Watch another application and if it closes close my app (without polling) c#

≡放荡痞女 提交于 2019-12-04 13:26:12

If you have the Process instance for the running application you can use Process.WaitForExit which will block until the process is closed. Of course you can put the WaitForExit in another thread so that your main thread does not block.

Here is an example, not using a separate thread

Process[] processes = Process.GetProcessesByName("notepad");
if (processes.Length > 0)
{
  processes[0].WaitForExit();
}

Here is a simple version using a thread to monitor the process.

public static class ProcessMonitor
{
  public static event EventHandler ProcessClosed;

  public static void MonitorForExit(Process process)
  {
    Thread thread = new Thread(() =>
    {
      process.WaitForExit();
      OnProcessClosed(EventArgs.Empty);
    });
    thread.Start();      
  }

  private static void OnProcessClosed(EventArgs e)
  {
    if (ProcessClosed != null)
    {
      ProcessClosed(null, e);
    }
  }
}

The following Console code is an example of how the above can be used. This would work equally well in a WPF or WinForms app of course, BUT remember that for UI you cannot update the UI directly from the event callback because it it run in a separate thread from the UI thread. There are plenty of examples here on stackoverflow explaining how to update UI for WinForms and WPF from a non-UI thread.

static void Main(string[] args)
{
  // Wire up the event handler for the ProcessClosed event
  ProcessMonitor.ProcessClosed += new EventHandler((s,e) =>
  {
    Console.WriteLine("Process Closed");
  });

  Process[] processes = Process.GetProcessesByName("notepad");
  if (processes.Length > 0)
  {
    ProcessMonitor.MonitorForExit(processes[0]);
  }
  else
  {
    Console.WriteLine("Process not running");        
  }
  Console.ReadKey(true);
}

That is easily possible because in Window's a process handle is waitable. Use the Process class to get the process's Handle.

Then use that to construct a SafeHandle, create a WaitHandle. Either wait on that yourself or register with ThreadPool.RegisterWaitForSingleObject() to get a call back when the process closes.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!