怎么判断进程已经死了啊?进程是我自己调用的外部程序

解决方案 »

  1.   

    http://blog.csdn.net/jiangsheng/archive/2004/12/31/235432.aspx
      

  2.   

    // 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;
      

  3.   

    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;