How to tell if a process is not Responding

Simple example of using built-in methods of the Process class to tell if an application is responding, and optionally kill the process.



namespace ProcRunning
{
using System;
using System.Diagnostics;

internal class Test
{
[STAThread]
private static void Main(string[] args)
{
Process.Start("IEXPLORE");
Console.WriteLine(ProcRunningUtil.IsProcRunning("IEXPLORE", false).ToString());
Console.WriteLine("any key to quit.");
Console.ReadLine() ;
}
}

public class ProcRunningUtil
{
private ProcRunningUtil(){}

public static bool IsProcRunning(string procName, bool killIt)
{
bool ok = false;
Process[] procs = Process.GetProcessesByName(procName);
try
{
if (procs[0].Responding)
{
Console.WriteLine(procs[0] + " is responding");
ok = true;
}
else
{
Console.WriteLine(procs[0] + " is not responding");
}
}
catch
{
Console.WriteLine(procName + " is not running");
}
//Close it:
if(killIt)
{
try
{
if (procs[0].Responding)
{
procs[0].CloseMainWindow();
}
else
{
//Force closure.
procs[0].Kill();
}
}
catch
{
Console.WriteLine("Could Not Find the " + procName + " Process");
}
}
return ok;
}
}
}


Submission Date:  12/6/2005 12:43:25 PM
Submitted By:  Peter Bromberg
My Home Page:  http://www.eggheadcafe.com

By Peter Bromberg   Popularity  (437 Views)