You need to
watch one or more processes to determine whether they have stopped
responding to the system. This functionality is similar to the column
in the Task Manager that displays the text
Responding
or Not Responding
,
depending on the state of the application.
Use the following method to determine whether a process has stopped responding:
public bool IsProcessResponding(Process process)
{
if (process.MainWindowHandle == IntPtr.Zero)
{
Console.WriteLine("This process does not have a MainWindowHandle");
return (true);
}
else
{
// This process has a MainWindowHandle
if (!process.Responding)
{
Console.WriteLine("Process " + process.ProcessName +
" is not responding.");
return (false);
}
else
{
Console.WriteLine("Process " + process.ProcessName +
" is responding.");
return (true);
}
}
}
The IsProcessResponding
method accepts a single
parameter, process
, identifying a process.
The Responding
property is then called on the
Process
object represented by the
process
parameter. This property returns a
true
to indicate that a process is currently
responding, or a false
to indicate that the
process has stopped responding.
The Responding
property always returns true
if the process in
question does not have a MainWindowHandle
.
Processes such as Idle, spoolsv, Rundll32, and svchost do not have a
main window handle and therefore the Responding
property always returns true
for them. To weed out
these processes, you can use the MainWindowHandle
property of the Process
class, which returns the
handle of the main window for a process. If this property returns
zero, the process has no main window.
To determine whether all processes on a machine are responding, you
can call the IsProcessResponding
method as
follows:
foreach (Process proc in Process.GetProcesses( )) { if (!MyObject.IsProcessResponding(proc)) { Console.WriteLine("Process " + proc.ProcessName + " is not responding."); } }
This code snippet iterates over all processes currently running on
your system. The static GetProcesses
method of the
Process
class takes no parameters and returns an
array of Process
objects that contains process
information for all processes running on your system. Each
Process
object is then passed in to our
IsProcessResponding
method to determine whether it
is responding. Other static methods on the Process
class that retrieve Process
objects are
GetProcessById
,
GetCurrentProcess
, and
GetProcessesByName
.