Monitor Processes and CPU Usage

April 16, 2008 15:48 by John M

This is a simple way in .NET 2.0 to monitor processes on your machine or a remote machine.  This example also monitors how much cpu usage the process is using at the time of monitoring.  Keep in mind this example also kills the process if it goes over 10% CPU Usage.

private static void killProcess()
        {
            Console.WriteLine("Watching to Kill Process!");
            int intInterval = 1000; // 1 second
            string procName = "YourProcessName";

            while (true)
            {

                Process[] runningNow = Process.GetProcesses();


                foreach (Process process in runningNow)
                {
                    using (PerformanceCounter pcProcess = new PerformanceCounter("Process", "% Processor Time", process.ProcessName))
                     {
                         if (process.ProcessName == procName)
                         {
                             pcProcess.NextValue();
                             System.Threading.Thread.Sleep(1000);
                             Console.WriteLine("Process:{0} CPU% {1}", process.ProcessName, pcProcess.NextValue());
                             if(pcProcess.NextValue() > float.Parse("10"))
                             {
                                 Console.WriteLine(string.Format("Killing {0} at {1}", procName, DateTime.Now.ToString()));
                                 process.Kill();
                             }
                         }
                    }
                }

                // Sleep till the next loop
                Thread.Sleep(intInterval);

            }


        }

Make sure you reference the following:

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Collections;

Anyway this is cool if you are needing to send alerts if a program takes up too much CPU usage or whatever the case may be.  Also .GetProcesses() and PerformanceCounter() have overloads that except the remote computer name.


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:
Categories: C#
Actions: E-mail | Permalink | Comments (7) | Comment RSSRSS comment feed

Related posts

Comments

April 16. 2008 22:23

trackback

Trackback from DotNetKicks.com

Monitor Process and CPU Usage

DotNetKicks.com

September 19. 2008 08:51

Gravatar

nice sharing this codes, i love this thanks..

laptop cases

October 1. 2008 09:08

Gravatar

What a nice post... very interesting.. thanks for this my friend...

busby seo test

October 20. 2008 09:51

Gravatar

Great code about monitoring cpu

seo test

December 8. 2008 09:53

Gravatar

How shall I modify the code to get a message "The process is over 10% of CPU" instead of killing the process?

Website monitoring

December 8. 2008 11:59

Gravatar

Instead of this in the if statement change it to you want

Console.WriteLine(string.Format("Killing {0} at {1}", procName, DateTime.Now.ToString()));
process.Kill();


Could be

Console.WriteLine(string.Format("{0} is over 10% utilization", procName));

Or you could write to a file of some sort like flat file or xml file for logging.

Hope this helps!

John_M

December 22. 2008 03:07

Gravatar

Hi John,

This really helps a lot. Thank you!

Website monitoring

Add comment


(Will show your Gravatar icon)  

  Country flag




Live preview

July 3. 2009 21:05

Gravatar