Home of the AlmostImplementedException

Creating a process monitor

In a previous post I wrote about debugging another application with mdbg. In this context I started to build an application that is able to log exceptions in other applications. But in some cases you can’t be sure that the monitored application is always running. So whats about e.g. small apps started by task scheduler? In this case you have to know when it’s started and when it’s ended – you will need the name of the application for this. The problematic aspect for this solution is that you can’t differentiate multiple instances.
Today I will show you a solution for this issue with explanation and source code. Please excuse that I will not provide a full step by step instruction.

As I mentioned you will first need the name of the application (you also can modify it more strictly by using the full path as identifier).
So we can start to build a class for the process observation with the not changeable name as unique identifier. At next build a method that determines the state of your process and assign the needed members. In my code I seperate between 3 states: started, ended, switched. Last one happens when the current monitored process ended but another instance is available for monitoring. This method should have the ability to run as thread – so keep an eye on thread-safety. This is the simplified functionality of the ObservableProcess class.

Now it’s getting more interesting. Imagine that you have to monitor multipile instances that can random be started or ended but you want be sure that one instance is always monitored by only one ObservableProcess. In my solution I will make use of a static Queue class that register the instace to the ObservableProcess. Simplified we have a List of the registered processId, and functions for registering and unregistering with a return value true=success and false=failed. The register functions takes an available process with the specified name(our unique id) that doesn’t exist in the queue, register it and assign the process to the ObservableProcess.

This should explain the functionality.
Now here is the source code that hopefully contains some helpful.

Share :

Leave a Reply

Your email address will not be published. Required fields are marked *