user1304843
user1304843

Reputation: 1

How to get notification when any process(.exe) ex. notepad exits in C#?

How to get notification when any process(.exe) ex. notepad is closed in C#?

Upvotes: 0

Views: 1083

Answers (1)

Avner Shahar-Kashtan
Avner Shahar-Kashtan

Reputation: 14700

The simplest way is to create a System.Diagnostics.Process for the Notepad process you want to monitor:

  Process p = Process.GetProcessesByName("notepad.exe").First();
  p.Exited += (sender, args) => Debug.WriteLine("Process has exited!");

Theoretically, you can do this for all running processes (with Process.GetProcesses()), but this won't give you the ability to be notified for any FUTURE processes, just the ones running when you start the monitoring.

A more low-level solution that could give you information on any process/window being closed is using CBT Hooks, with a .NET wrapper supplied here, but this requires a lot more manual management, since it gives you information on closing Win32 window, not only processes.

Upvotes: 9

Related Questions