Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to check whether an application is running or not responding Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
04-Oct-02
Category
Win API
Language
Delphi 2.x
Views
92
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius 

I'm in the process of writing a service application that needs to check the status 
of a couple of running programs - so that it can kill/ restart the programs if they 
become unresponsive. I'm using CreateProcess to start the applications and 
terminateprocess to kill the applications. What API call can I use to check the 
status of a running application such as "Running" or "Not Responding" like 
Taskmanager does?

Answer:

Use SendMessageTimeout to send a message to the window. If the message times out 
after, say, 10 seconds, you can assume the applcation is not responding (the task 
manager assumes this after 5 seconds).

1   
2   procedure TForm1.Button1Click(Sender: TObject);
3   var
4     Res: DWORD;
5     H: HWND;
6   begin
7     H := FindWindow(nil, 'FormSomeCaption');
8     if H = 0 then
9       Label1.Caption := 'Window not found'
10    else if SendMessageTimeout(H, WM_NULL, 0, 0, SMTO_NORMAL, 100, Res) <> 0 then
11      Label1.Caption := 'Responding'
12    else
13      Label1.Caption := 'Not responding';
14  end;



And to kill it, no questions asked, then

15  
16  procedure TForm1.ButtonClick(Sender: TObject);
17  var
18    ProcessHandle: THandle;
19    WinHwnd: HWND;
20    ProcessID, ExitCode: Integer;
21  begin
22    ProcessID := 0;
23    ExitCode := 0;
24    WinHwnd := FindWindow(nil, 'FormSomeCaption');
25    if not (IsWindow(WinHwnd)) then
26    begin
27      ShowMessage('Window not found');
28      exit;
29    end;
30    GetWindowThreadProcessID(WinHwnd, @ProcessID);
31    ProcessHandle := OpenProcess(PROCESS_CREATE_THREAD or PROCESS_VM_OPERATION
32      or PROCESS_VM_WRITE or PROCESS_VM_READ or PROCESS_TERMINATE,
33      False, ProcessID);
34    if (ProcessHandle > 0) then
35    begin
36      GetExitCodeProcess(ProcessHandle, ExitCode);
37      { or  GetExitCodeProcess(ProcessHandle, DWORD(ExitCode)); }
38      TerminateProcess(ProcessHandle, ExitCode);
39      CloseHandle(ProcessHandle);
40    end
41    else
42      ShowMessage('Unable to get proccess Handle');
43  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC