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?
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:
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: