您现在的位置:首页 >> 基础算法 >> window基础 >> 内容

Delphi中判断程序是否无响应

时间:2011/9/3 15:32:34 点击:

  核心提示:function IsAppRespondig9X(dwThreadId: DWORD): Boolean;typeTIsHungThread = function(dwThreadId: DWORD...
function IsAppRespondig9X(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;

function IsAppRespondigNT(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 IsAppRespondig(Wnd: HWND): Boolean;
begin
  if not IsWindow(Wnd) then
  begin
    ShowMessage('Incorrect window handle!');
    Exit;
  end;
  if Win32Platform = VER_PLATFORM_WIN32_NT then
    Result := IsAppRespondigNT(wnd)
  else
    Result := IsAppRespondig9X(GetWindowThreadProcessId(Wnd,nil));
end;

//实例
procedure TForm1.Button1Click(Sender: TObject);
var
  Res: DWORD;
  h: HWND;
begin
  h := FindWindow(nil, 'notepad');
  if h > 0 then
  begin
    if IsAppRespondig(h) then
      ShowMessage('notepad 有响应')
    else
      ShowMessage('notepad 无响应');
  end
  else
    ShowMessage('未打开 notepad');
end;

作者:网络 来源:转载
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
  • 盒子文章 技术支持:深圳市麟瑞科技有限公司(www.2ccc.com) © 2024 版权所有 All Rights Reserved.
  • 粤ICP备10103342号-1