Determining Whether a Process Has Stopped Responding

//Determining Whether a Process Has Stopped Responding

Description


This code snippet will help you to determine your process state.

Code





#undef TRACE

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;


public enum ProcessRespondingState
{
Responding,
NotResponding,
Unknown
}

static void Main(string[] args)
{
ProcessRespondingState state;
foreach (Process p in Process.GetProcesses())
{
state = IsProcessResponding(p);
if (state == ProcessRespondingState.NotResponding)
{
Console.WriteLine("{0} Process not responding", p.ProcessName);
}
if (state == ProcessRespondingState.Responding)
{
Console.WriteLine("{0} process responding", p.ProcessName);
}
if (state == ProcessRespondingState.Unknown)
{
Console.WriteLine("{0} process state unknown", p.ProcessName);
}
}
Console.Read();
}

public static ProcessRespondingState IsProcessResponding(Process process)
{
if (process.MainWindowHandle == IntPtr.Zero)
{
Trace.WriteLine("{0} does not have a MainWindowHandle",
process.ProcessName);
return ProcessRespondingState.Unknown;
}
else
{
// This process has a MainWindowHandle.
if (!process.Responding)
{
Trace.WriteLine("{0} is not responding.", process.ProcessName);
return ProcessRespondingState.NotResponding;
}
else
{
Trace.WriteLine("{0} is responding.", process.ProcessName);
return ProcessRespondingState.Responding;
}
}
}


}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: