0

I need to check if a process is closed/killed. Then run some clean up code.

Example scenario.

If a user closes word I would like to fire off some clean up code.

  private void createhandler()
    {

        try
        {
            foreach (Process proc in Process.GetProcessesByName("WINWORD"))
            {
              proc.Exited += new EventHandler(processExited);


            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }


    }

    private void processExited(object sender, EventArgs e)
    {
        MessageBox.Show("closed");

    }
Shezad Ahmed
  • 69
  • 1
  • 6
  • 1
    Is there something wrong with the code you have provided? – GSerg Jun 28 '21 at 12:30
  • 1
    When I close word manully it does not fire off the processExited . I don't get a messagebox – Shezad Ahmed Jun 28 '21 at 12:31
  • 1
    @GSerg It won't work: the events only fire if `EnableRaisingEvents` is true, and that can only be set if the process was started from .NET. There's no way of getting an event when some other process ends: you'll have to poll – canton7 Jun 28 '21 at 12:32
  • 1
    Huh, turns out you can set `EnableRaisingEvents` on a pre-existing process – canton7 Jun 28 '21 at 12:39

0 Answers0