.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

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

Most Popular Programming Stories

More for Developers

RSS Feeds