Mega Code Archive

 
Categories / Delphi / Examples
 

Recognize frozen applications [watchdog]

Question: I am developing a sort of watchdog and I need to programatically check from an application, if another application is not responding. How can I do this? Answer: Try by sending a message with SendMessageTimeOut. If the another application is not responding, there is a timout. The example below searches for application 'MyApplication' in the main window's caption bar and gives it 3 seconds to respond to a message. function AppFrozen(H: HWND) : Boolean; var dwResult: DWord; const timeout = 3000; // ms begin AppFrozen := SendMessageTimeout(H, WM_NULL, 0, 0, SMTO_ABORTIFHUNG or SMTO_BLOCK, timeout, dwResult) <> 0 end; var H: HWND; begin // enumerate top level windows or search for an // application that you are monitoring H := FindWindow(nil, 'MyApplication'); if H<>0 then if AppFrozen(H) then ShowMessage('MyApplication is Frozen!') end.