Find a Running Process in .NET

Disclaimer: The sample code below is for educational purposes only. Do not rub on skin.

A few years back I wrote a class module in VB6 to allow an application to detect whether a specified process is currently running on the user's system. This required a lot of API code, and, while it gets the job done, it's a bit unwieldly, especially if you're debugging an app that uses it and you end up stepping through it's code rather than stepping over it. But there's a much easier way to do it in .NET, with only a few lines of code.

The answer lies in the System.Diagnostics namespace, and the Process class contained within. Why they put the Process class in the Diagnostics namespace is beyond me -- sure, you can use it for diagnostics, but also for, among other things, executing processes. But for this example, we'll focus simply on determining if a specified process is running.

First, you'll need to add a reference to the aforementioned System.Diagnostics namespace. Then add a function where you can create an array of Process objects, and initialize them by way of the static Process.GetProcessesByName() function. Check out the code below:

private void btnCheckForProcess_Click(object sender, System.EventArgs e)
{
   Process[] processes = null;

   this.Cursor = Cursors.WaitCursor;
   processes = Process.GetProcessesByName("winlogon");
   this.Cursor = Cursors.Default;

   if( processes.Length > 0 )
      MessageBox.Show("winlogon StartTime = " + processes[0].StartTime.ToString());
   else
      MessageBox.Show("Process not found!");

   processes = null;
}

The example above declares an array of Process objects, then initializes it as described by using the static Process.GetProcessesByName() function. In this case, we are looking for the winlogon process. This can take a couple of seconds, so I set the cursor settings appropriately around the function call so that the user would know that something's going on and the program isn't just chillin'. If the process is found, the app displays it's start time, otherwise it displays a message stating that the process was not found. There should only be one instance of winlogon running, but in other cases you may end up finding more than one instance of the process you're looking for. And even though in this example we're displaying the process' start time, the Process class has many useful properties relating to the process. As I mentioned early, you can use the Process class for, among other things, starting a process, as well as other things you'll probably find useful.

Comments

Anonymous said…
Hi,
Thanks for the nice article.I have a question here,can we find a IEXPLORE process based on which URL it is running. For exaple there are 3 web pages currently open on my desktop , one of them is www.google.com.So how to programatically find this process.

Regards,
Su

Popular Posts

Resolving the "n timer(s) still in the queue" Error In Angular Unit Tests

How to Get Norton Security Suite Firewall to Allow Remote Desktop Connections in Windows

How to Determine if a Column Exists in a DataReader

Fixing the "Please add a @Pipe/@Directive/@Component annotation" Error In An Angular App After Upgrading to webpack 4

Silent Renew and the "login_required" Error When Using oidc-client