Solved

Closing a Application

Posted on 2000-04-18
13
147 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
ID: 2726607
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
ID: 2726620
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
ID: 2726687
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
ID: 2726710
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
ID: 2726770
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
ID: 2726772
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:bryan7
ID: 2729010
listenning
0
 
LVL 2

Expert Comment

by:craig_capel
ID: 2729033
snoopy, does that code work in NT??????????????????
0
 
LVL 1

Expert Comment

by:msedi
ID: 2731004
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
ID: 2731088
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
ID: 2732988
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
ID: 2733690
Hello,

sorry, I don't know.
0
 
LVL 2

Expert Comment

by:craig_capel
ID: 2733895
works fine on 95 / 98 systems....
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

911 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

16 Experts available now in Live!

Get 1:1 Help Now