Solved

Closing a Application

Posted on 2000-04-18
13
145 Views
Last Modified: 2010-04-04
How can i close a application, when it doesn't response. I have written a little prog, which checks every minute, if the application is alive, and when th eapplication didn't answer, i would like to kick it, and start again !
0
Comment
Question by:snoopy_Spy
  • 4
  • 3
  • 2
  • +3
13 Comments
 
LVL 7

Expert Comment

by:Motaz
Comment Utility
What did you mean exactly?
if you mean that some application stay as a gaurd for another appliction I can help you.

You can chech the other application existence such as:

var
  H: hwnd;
begin
  H:= FindWindow('TfmMain', 'Printer reports');

  if H = 0 then
    ShellExecute(handle, 'open',
      PChar(ExtractFilePath(ParamStr(0)) + 'Traffic.exe'),
        nil, nil, sw_ShowMinimized);

This code exists in a timer and it checks if the other application exists or not (TfmMain) if it didn't exists it will run it.

The parameters:

  FindWindow(MainWindowsClassName, Caption_of_Main_Window);

Motaz
0
 
LVL 1

Expert Comment

by:tomer_engel
Comment Utility
what motaz  suggested is very good.

you can implement another approch--the original application can send messages(acostomed message)to athread of  his (each minute).the thread "knows" that  it should get messages every 1 min. if it doesnt get it apply the close procedure,and restart the     applciation
0
 

Author Comment

by:snoopy_Spy
Comment Utility
I don't need the routine for checking if the application is alive ! - This works allready fine.
I need to shut down the application if it hangs !
Sending the application a WM_CLOSE or something else doesn't work - only closeing it with the Windows NT TaskManager works ! - So how can i close the Process from my programm ?
0
 
LVL 7

Expert Comment

by:Motaz
Comment Utility
I think the best way is to prevent your application from hangin, use Try except in the code that might cause that hanging.

You can post your code here to see what is the problem.

Hanging is a problem in your code.

Motaz
0
 
LVL 2

Expert Comment

by:craig_capel
Comment Utility
var
wnd : hwnd;
ProcessID,cpid : Cardinal;
begin
wnd:=form1.handle; //handle of window  (in this case findwindow(nil,'title of your program')
ProcessID := GetWindowThreadProcessID(wnd,@cpid);
TerminateProcess(OpenProcess(PROCESS_TERMINATE,false,cpid),1);
end;

and that will simulate alt ctrl and del on it.....

Craig C.
0
 

Author Comment

by:snoopy_Spy
Comment Utility
This isn't my programm !
I only wrote the programm which checks, if the application is working fine. But when the application hangs, i would like to close the task or the process !
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 3

Expert Comment

by:bryan7
Comment Utility
listenning
0
 
LVL 2

Expert Comment

by:craig_capel
Comment Utility
snoopy, does that code work in NT??????????????????
0
 
LVL 1

Expert Comment

by:msedi
Comment Utility
Hello,

First I think it's not a good way to call TerminateThread or TerminateProcess. These functions are very rude and will kick out your app without freeing memory or so. In this case you will get some day a out of memory error?
For your message timeout: There is a windows message something like WM_..._TIMEOUT, but I don't know the exact message name. This message is sent from windows to the app in order to check whether the app is still ok or not. That's what you will see in your task list in WinNT if it's responding or not

Another question: Why will this application hang so often?

Good luck, Martin
0
 
LVL 2

Accepted Solution

by:
craig_capel earned 50 total points
Comment Utility
msedi, if an app has hung, it can not release the memory back to windows anyhow... Terminating it from NT does that to put back the memory?... I don't see how...

There is no other way, at least i don't think ....
0
 

Author Comment

by:snoopy_Spy
Comment Utility
Yes it works !
Thanxs
But, do you know if this works under Win9X or Win2K ? I don't use them, but i would like to know it ...
0
 
LVL 1

Expert Comment

by:msedi
Comment Utility
Hello,

sorry, I don't know.
0
 
LVL 2

Expert Comment

by:craig_capel
Comment Utility
works fine on 95 / 98 systems....
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now