Take the tour ×

Question born of curiosity here:

How would I go about being notified or polling, to see when a process is currently being terminated?

So my application could listen out for a "termination begin" event from a process, for example, to track how long a particular process takes to close on average?

Note, this isn't in conjunction with my process flipping the kill switch, that would be simple yes, but my application would act only as an observer.

Any help would be greatly appreciated :)

share|improve this question
add comment

3 Answers

The only way I know for you to do this without polling is through WMI. There are plenty of examples on the web, e.g. Using WMI to monitor process creation, deletion and modification in .NET.

WMI is well wrapped by the .net libraries. For example, the sample above uses ManagementEventWatcher to subscribe to WMI events. There's a whole treasure trove of WMI goodness in the System.Management namespace.

share|improve this answer
+1. yeah completely forgot it is all exposed in WMI. –  Aliostad Nov 28 '11 at 14:08
@Aliostad WMI, the oft forgotten gem of Windows...... –  David Heffernan Nov 28 '11 at 14:13
add comment

I'm not really sure if there is a way for this to be a "push" operation (you somehow get notified of the ending process).

But what I would do to mimick this observation is to create a Windows Service that polls the process on a specified interval (say, every 2 seconds or however much latency is acceptable for you). That way you can handle the situation when the other process ends and act accordingly.

share|improve this answer
again, polling is not needed –  David Heffernan Nov 28 '11 at 13:42
add comment

I am not sure if there is a WIN32 API for subscribing to events of the process terminated but you can do the polling simply by getting the list of processes running:

 var processes = System.Diagnostics.Process.GetProcesses();

I would use the ProcessId as the key for my dictionary to check when it is started and when it is terminated.

share|improve this answer
No need for polling. Use WMI instead. –  David Heffernan Nov 28 '11 at 13:42
add comment

Your Answer


