How does delphi determine if the application is not responding?

Source: Internet
Author: User
Today, I saw two articles in the core discussion group of MSN, discussing whether the application did not respond. The original Article is as follows:

> 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.

--
Rational. of course, I also have an unmarshented function here, which is another solution. nt and 9x have a user32.dll function, ishungappwindow (NT) and ishungthread (9x ). easy to use. the prototype is given below.
Bool ishungappwindow (
Hwnd, // handle to main app's window
);

Bool ishungthread (
DWORD dwthreadid, // The thread's identifier of the main app's window
);
With the prototype, you don't need any explanation. It's too bad. :) However, you need to call the function in getprocaddress. The Library does not have this function.
****************************************
Check whether an application (window) is not responding?

{1. The specified ented way}

{
An application can check if a window is responding to messages
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 unencrypted ented 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
Determine if the target application is responding but CILS
Uninitialized ented 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)
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
User32.lib for these functions, so we shoshould load them dynamically using
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 ('recorct 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;
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.