http://www.cnblogs.com/smallmuda/archive/2009/07/24/1529845.html

delphi 如何判断应用程序未响应

 
 今天在MSN的核心讨论组上看到两篇文章.讨论的乃是应用程序是否没有响应.原文如下:     
    
  >   How   is   it   possible   to   determine   a   process   is   "not   responding"   like   NT   Task     
  >   Manager   do?     
  The   heuristic   works   only   for   GUI   processes,   and   consists   of   calling     
  SendMessageTimeOut()   with   SMTO_ABORTIFHUNG.     
    
  >There   is   any   API   call   to   do   the   job,   or   this   status   is   simply   a   deduction     
  >based   on   process   counters,   like   that   returned   from   call   to   GetProcessTimes     
  >API   function?     
    
  Use   SendMessageTimeout   with   a   value   of   WM_NULL.   That's   all   Task     
  Manager   does   to   determine   this   AFAIK.     
    
  --     
  有理有理.当然,我这里还有一个UNDOCUMENTED函数,乃是其他的解决方案,NT和9X有个USER32.DLL的函数,IsHungAppWindow(NT)和IsHungThread(9X).使用起来简便无比.下面给出原型.     
  BOOL   IsHungAppWindow   (     
  HWND   hWnd,   //   handle   to   main   app's   window     
  );     
    
  BOOL   IsHungThread   (     
  DWORD   dwThreadId,   //   The   thread's   identifier   of   the   main   app's   window     
  );     
  有了原型,连解释都不需要,好得不的了.:)不过调用时需要GetProcAddress.库里没有该函数.     
  ****************************************   
  check   whether   an   application   (window)   is   not   responding?   
    
  {1.   The   Documented   way}     
    
  {     
      An   application   can   check   if   a   window   is   responding   to   messages   by     
      sending   the   WM_NULL   message   with   the   SendMessageTimeout   function.     
  }     
    
  function   AppIsResponding(ClassName:   string):   Boolean;     
  const     
      {   Specifies   the   duration,   in   milliseconds,   of   the   time-out   period   }     
      TIMEOUT   =   50;     
  var     
      Res:   DWORD;     
      h:   HWND;     
  begin     
      h   :=   FindWindow(PChar(ClassName),   nil);     
      if   h   <>   0   then     
          Result   :=   SendMessageTimeOut(H,     
              WM_NULL,     
              0,     
              0,     
              SMTO_NORMAL   or   SMTO_ABORTIFHUNG,     
              TIMEOUT,     
              Res)   <>   0     
      else     
          ShowMessage(Format('%s   not   found!',   [ClassName]));     
  end;     
    
  procedure   TForm1.Button1Click(Sender:   TObject);     
  begin     
      if   AppIsResponding('OpusApp')   then     
          {   OpusApp   is   the   Class   Name   of   WINWORD.EXE   }     
          ShowMessage('App.   responding');     
  end;     
    
  {2.   The   Undocumented   way}     
    
  {     
      //   Translated   form   C   to   Delphi   by   Thomas   Stutz     
      //   Original   Code:     
      //   (c)1999   Ashot   Oganesyan   K,   SmartLine,   Inc     
      //   mailto:ashot@aha.ru,   http://www.protect-me.com,   http://www.codepile.com     
    
    The   code   doesn't   use   the   Win32   API   SendMessageTimout   function   to     
    determine   if   the   target   application   is   responding   but   calls     
    undocumented   functions   from   the   User32.dll.     
    
    -->   For   Windows   95/98/ME   we   call   the   IsHungThread()   API     
    
    The   function   IsHungAppWindow   retrieves   the   status   (running   or   not   responding)     
    of   the   specified   application     
    
    IsHungAppWindow(Wnd:   HWND):   //   handle   to   main   app's   window     
    BOOL;     
    
    -->   For   NT/2000/XP   the   IsHungAppWindow()   API:     
    
    The   function   IsHungThread   retrieves   the   status   (running   or   not   responding)   of     
    the   specified   thread     
    
    IsHungThread(DWORD   dwThreadId):   //   The   thread's   identifier   of   the   main   app's   window     
    BOOL;     
    
    Unfortunately,   Microsoft   doesn't   provide   us   with   the   exports   symbols   in   the     
    User32.lib   for   these   functions,   so   we   should   load   them   dynamically   using   the     
    GetModuleHandle   and   GetProcAddress   functions:     
  }     
    
  //   For   Win9X/ME     
  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;     
    
  //   For   Win   NT/2000/XP     
  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;     
    
  //   Example:   Check   if   Word   is   hung/responding     
    
  procedure   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   IsAppRespondig(h)   then     
              ShowMessage('Word   is   responding!')     
          else     
              ShowMessage('Word   is   not   responding!');     
      end     
      else     
          ShowMessage('Word   is   not   open!');     
  end;     

delphi 如何判断应用程序未响应的更多相关文章

  1. win7系统程序未响应怎么办

    问题描述:出现“程序未响应...”而后系统程序就没有反应了. 解决方案:1.运行→输入“regedit”→hkey_current_usser/control panel/desktop/window ...

  2. Windows应用程序未响应

    昨天在安装postgresql的扩展功能postgis的时候,stackbuilder刚打开就死掉,一直未响应,刚开始以为是内存的原因,后来发现并没有占用太多内存,最后打开vpn发现就可以了,原来是网 ...

  3. 【转】VS2013 C#WinForm程序构造界面拖动控件NumericUpDown时"未响应“是有道词典惹的祸

    很久之前遇到过因为金山词霸和其他软件冲突导致的程序无响应的情况. 没想到今天情况重现,VS2013在可视化编辑NumbericUpDown控件的时候,又出现了”未响应“,发现又是有道词典惹的祸. 可见 ...

  4. 软件看门狗--别让你地程序无响应(使用未公开API函数IsHungAppWindow,知识点较全)

    正文一.概述一些重要的程序,必须让它一直跑着:而且还要时时关心它的状态——不能让它出现死锁现象.当然,如果一个主程序会出现死锁,肯定是设计或者编程上的失误.我们首要做的事是,把这个Bug揪出来.但如果 ...

  5. Delphi如何处理在进行大量循环时,导致的应用程序没有响应的情况

    一般用在比较费时的循环中,往往导致应用程序没有响应,此时在比较费时的程序体中加入Application.ProcessMessages即可解决,该语句的作用是检查并先处理消息队列中的其他消息. 例如, ...

  6. 关于Qt Designer程序/UI文件打开未响应的解决方法

    最近完成一个项目,到最后关头用QtCreator无法打开UI文件,每次都未响应,用QtDesigner也无法启动 这个问题把我折磨了半天,最后才知道原来是要删除C:\Users\Administrat ...

  7. [转]Delphi中,让程序只运行一次的方法

    program onlyRunOne; uses Forms,Windows,SysUtils, Dialogs, Unit1 in 'Unit1.pas' {Form1}; {$R *.res} v ...

  8. timeout Timeout时间已到.在操作完成之前超时时间已过或服务器未响应

    Timeout时间已到.在操作完成之前超时时间已过或服务器未响应 问题 在使用asp.net开发的应用程序查询数据的时候,遇到页面请求时间过长且返回"Timeout时间已到.在操作完成之间超 ...

  9. 超时时间已到。在操作完成之前超时时间已过或服务器未响应。 (.Net SqlClient Data Provider)

    超时时间已到.在操作完成之前超时时间已过或服务器未响应. (.Net SqlClient Data Provider) 在做一个小东西的时候出现了这个问题,就是使用VS调试几次项目后,使用SQL Se ...

随机推荐

  1. 洛谷P2296寻找道路

    传送门啦 题目中有一个条件是路径上的所有点的出边所指向的点都直接或间接与终点连通. 所以我们要先判断能否走这一个点, $ bfs $ 类似 $ spfa $ 的一个判断,打上标记. 在这我反向建图,最 ...

  2. ZOJ 3781 Paint the Grid Reloaded(DFS连通块缩点+BFS求最短路)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5268 题目大意:字符一样并且相邻的即为连通.每次可翻转一个连通块X( ...

  3. Unix IPC之互斥锁与条件变量

    互斥锁 1.函数声明 #include <pthread.h> /* Mutex handling. */ /* Initialize a mutex. */ extern int pth ...

  4. 学习python绘图

    学会python画图 # 使用清华的pip源进行安装sklearn # pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -U sciki ...

  5. CentOS下用yum命令安装jdk

    一.使用yum命令安装 1.查看是否已安装JDK,卸载 [root@192 ~]# yum list installed |grep java java-1.8.0-openjdk.x86_64    ...

  6. Jenkins+Ant+Git+Jmeter实现持续集成

    个人记录: 基本的配置与Jenkins+Ant+SVN+Jmeter实现持续集成的配置一样,主要在Jenkins的配置上的区别会有所不同 安装的插件: enkins安装好之后,需要为其安装gitlab ...

  7. python连接hbase

    安装HBase HBase是一个构建在HDFS上的分布式列存储系统,主要用于海量结构化数据存储.这里,我们的目标只是为Python访问HBase提供一个基本的环境,故直接下载二进制包,采用单机安装.下 ...

  8. Pytest里,mark装饰器的使用,双引号,没引号,这种差别很重要

    按最新版的pytest测试框架. 如果只是单一的mark,不要加任何引号. 如果是要作and ,not之类的先把,一定要是双引号! 这个要记清楚,好像和以前版本的书上介绍的不一样,切记! import ...

  9. Vue.js中 watch(深度监听)的最易懂的解释[转]

    https://blog.csdn.net/qq_36688143/article/details/81287535 taskData: { handler(v) { // watch 方法其实默认写 ...

  10. bzoj 1899 贪心+dp

    思路:这个贪心排顺序我居然没看出来. 吃饭时间长的在前面, 用反证法很容易得出. 剩下的就是瞎dp啦. #include<bits/stdc++.h> #define LL long lo ...