.NET Tip: It's 2:00 a.m. Do You Know What Your Processes Are Doing?

I recently needed to check on the status of some processes running on a server. I simply logged into the server and fired up Task Manager. This worked fine for a quick one-time check. What is really needed, though, is an automated way to keep tabs on these processes. The .NET Framework has classes that make it very easy to retrieve the same information that is available on the Processes tab in Task Manager. Here is a function that takes a process and returns its current state:

public enum ProcessState
   Responding,
   NotResponding,
      Unknown
}

public static ProcessState GetProcessState(Process Proc)
{
   if (Proc.MainWindowHandle == IntPtr.Zero)
      return ProcessState.Unknown;
   else if (Proc.Responding)
      return ProcessState.Responding;
   else
      return ProcessState.NotResponding;
}

The function above then can be used to determine the state of all processes running on the server. The following code loops through all processes running on the machine and displays their name and status.

ProcessState State;
foreach (Process Proc in Process.GetProcesses())
{
   State = Util2008.GetProcessState(Proc);
   Debug.Print("{0}: {1}", Proc.ProcessName, State.ToString());
}

Here is the output for the first few processes running on my computer.

OUTLOOK: Responding xplore: Responding
k9filter: Unknown
vpngvpngui: Unknown

You now have everything you need to automate monitoring the processes. Simply check that state of the process on a periodic basis and then take the appropriate action if the state is not what is expected.

About the Author

Jay Miller is a Software Engineer with Electronic Tracking Systems, a company dedicated to robbery prevention, apprehension, and recovery based in Carrollton, Texas. Jay has been working with .NET since the release of the first beta and is co-author of Learn Microsoft Visual Basic.Net In a Weekend. Jay can be reached via email at jmiller@sm-ets.com.



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Event Date: April 15, 2014 The ability to effectively set sales goals, assign quotas and territories, bring new people on board and quickly make adjustments to the sales force is often crucial to success--and to the field experience! But for sales operations leaders, managing the administrative processes, systems, data and various departments to get it all right can often be difficult, inefficient and manually intensive. Register for this webinar and learn how you can: Align sales goals, quotas and …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds