Tag Archives: process stop event

How to know if a process has stopped or started using events in C#

There are numerous of ways to detect if a new process has started or stopped, sadly the majority of them are extremely inefficient as it requires you to keep looping through the active process constantly to see if a new one appeared in the array or if one is not there any more.

Luckily the windows Win32_ProcessStartTrace and Win32_ProcessStopTrace classes are here to help out.

The first thing we need to do is reference System.Management.dll in our project. Then we need to define the scope in your class which we will be using.

After that we need to initialise the class which will contain the process start and process stopped events and add the handlers and their methods.

Add the two following variables in your Class.

In your constructor the event handlers need to be added.

and then their event methods that will be trigged when a process either starts or stops.

And finally we need to start the events by using