// For Win9x/ME function IsAppResponding9x(dwThreadId: DWORD): Boolean; type TIsHungThread = function(dwThreadId: DWORD): BOOL; stdcall; var hUser32: THandle; IsHungThread: TIsHungThread; begin Result := True; hUser32 := GetModuleHandle('user32.dll'); if (hUser32 > 0) then begin @IsHungThread := GetProcAddress(hUser32, 'IsHungThread'); if Assigned(IsHungThread) then begin Result := not IsHungThread(dwThreadId); end; end; end;// For Win NT/2000/XP function IsAppRespondingNT(wnd: HWND): Boolean; type TIsHungAppWindow = function(wnd:hWnd): BOOL; stdcall; var hUser32: THandle; IsHungAppWindow: TIsHungAppWindow; begin Result := True; hUser32 := GetModuleHandle('user32.dll'); if (hUser32 > 0) then begin @IsHungAppWindow := GetProcAddress(hUser32, 'IsHungAppWindow'); if Assigned(IsHungAppWindow) then begin Result := not IsHungAppWindow(wnd); end; end; end;function IsAppResponding(Wnd: HWND): Boolean; begin if not IsWindow(Wnd) then begin ShowMessage('Incorrect window handle'); Exit; end; if Win32Platform = VER_PLATFORM_WIN32_NT then Result := IsAppRespondingNT(wnd) else Result := IsAppResponding9X(GetWindowThreadProcessId(wnd,nil)); end;// Example: Check if Word is hung/respondingprocedure TForm1.Button3Click(Sender: TObject); var Res: DWORD; h: HWND; begin // Find Word by classname h := FindWindow(PChar('OpusApp'), nil); if h <> 0 then begin if IsAppResponding(h) then ShowMessage('Word is responding') else ShowMessage('Word is not responding'); end else ShowMessage('Word is not open'); end;
function IsWindowResponding(Wnd: HWND; Timeout: Integer): Boolean; var Res: DWORD; begin Result := SendMessageTimeout(Wnd, WM_NULL, 0, 0, SMTO_ABORTIFHUNG, Timeout, Res) <> 0; end; var h: HWND; begin h := FindWindow('TfrmCZServerMain', nil); if h <> 0 then if not IsWindowResponding(h, 3000) then begin // KillProcess(h); end;
function IsAppResponding9x(dwThreadId: DWORD): Boolean;
type
TIsHungThread = function(dwThreadId: DWORD): BOOL; stdcall;
var
hUser32: THandle;
IsHungThread: TIsHungThread;
begin
Result := True;
hUser32 := GetModuleHandle('user32.dll');
if (hUser32 > 0) then
begin
@IsHungThread := GetProcAddress(hUser32, 'IsHungThread');
if Assigned(IsHungThread) then
begin
Result := not IsHungThread(dwThreadId);
end;
end;
end;// For Win NT/2000/XP
function IsAppRespondingNT(wnd: HWND): Boolean;
type
TIsHungAppWindow = function(wnd:hWnd): BOOL; stdcall;
var
hUser32: THandle;
IsHungAppWindow: TIsHungAppWindow;
begin
Result := True;
hUser32 := GetModuleHandle('user32.dll');
if (hUser32 > 0) then
begin
@IsHungAppWindow := GetProcAddress(hUser32, 'IsHungAppWindow');
if Assigned(IsHungAppWindow) then
begin
Result := not IsHungAppWindow(wnd);
end;
end;
end;function IsAppResponding(Wnd: HWND): Boolean;
begin
if not IsWindow(Wnd) then
begin
ShowMessage('Incorrect window handle');
Exit;
end;
if Win32Platform = VER_PLATFORM_WIN32_NT then
Result := IsAppRespondingNT(wnd)
else
Result := IsAppResponding9X(GetWindowThreadProcessId(wnd,nil));
end;// Example: Check if Word is hung/respondingprocedure TForm1.Button3Click(Sender: TObject);
var
Res: DWORD;
h: HWND;
begin
// Find Word by classname
h := FindWindow(PChar('OpusApp'), nil);
if h <> 0 then
begin
if IsAppResponding(h) then
ShowMessage('Word is responding')
else
ShowMessage('Word is not responding');
end
else
ShowMessage('Word is not open');
end;
var
Res: DWORD;
begin
Result := SendMessageTimeout(Wnd, WM_NULL, 0, 0, SMTO_ABORTIFHUNG, Timeout, Res) <> 0;
end;
var
h: HWND;
begin
h := FindWindow('TfrmCZServerMain', nil);
if h <> 0 then
if not IsWindowResponding(h, 3000) then
begin
// KillProcess(h);
end;