.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

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • CentreCorp is a fully integrated and diversified property management and real estate service company, specializing in the "shopping center" segment, and is one of the premier retail service providers in North America. Company executives travel a great deal, carrying a number of traveling laptops with critical current business data, and no easy way to back up to the network outside the office. Read this case study to learn how CentreCorp implemented a suite of business continuity services that included …

Most Popular Programming Stories

More for Developers

RSS Feeds