tags:

views:

71

answers:

3

I am creating an application that monitors a select group of programs and restarts them if required. With this group of programs if something has 'gone wrong' (i.e. they need restarted) the program will appear in task manager as 'not responding'.

Is there any way to iterate around non-responding programs in windows using an api? Or alternatively given I have the process id or handle to a window can I query whether it is responsive or not?

+3  A: 

I think this is what you're after.

Re the valid comment, something like:

Process[] processes = Process.GetProcessesByName("your process name");
if(!processes[0].Responding)
 // kill it or do something
Antony Koch
This will not work for remote processes, which I'd presume to be the case.
pierre
Amended to include further code.
Antony Koch
+2  A: 

Unresponsive applications are detected by sending a message to the application and checking if it gets processed within a few seconds. I do not know if there is a API call to do that but it is quite easy to implement on your own.

Daniel Brückner
+4  A: 

First, you must iterate through all top level windows. Do so by calling the Win32 EnumWindows. This function requires a callback routine, so it's tricker to explain in C#. Anyway, check the documentation.

Then on each iteration, call the function below passing the window handle:

[DllExport("user32.dll")]
static bool IsHungAppWindow(IntPtr theWndHandle);

Calling this function on a window should return true for a non-reponsive window. Then, receive the ID of the process by calling GetWindowThreadProcessId.

Then obtain a reference to a process and quit it:

Process aPrc = Process.GetProcessById(aPrcID);
aPrc.Kill();
Kerido