Link to home
Start Free TrialLog in
Avatar of steinonline
steinonline

asked on

Clean up after application, with on Terminate handler?

How do I create a single event handler that executes on terminate of a delphi application?  Regardless of how the application ends, I need a cleanup procedure to run,  especially if my program ends abnormally.  For performance purposes, my program ditches the windows wallpaper, remembers where the desktop icons are, and changes screen resolution to an optimum size for my program.  When my program finishes normally these are all seamlessly returned to normal, however when I end by some other means, such as the task manager or a hard shutdown, I am not able to restore these settings back to normal.  Is there any way I can accomplish this by simulating and application.OnTerminate event?

If so what windows message would I use for it?  I've tried many to no avail.
Avatar of Mike Littlewood
Mike Littlewood
Flag of United Kingdom of Great Britain and Northern Ireland image

You could code in the formClose of your application, but not sure if that will catch hard terminations.
You might have to plug into the windows message queue in your application to trap a WM_CLOSE message from windows, not sure if there is a WM_TERMINATE.
Avatar of steinonline
steinonline

ASKER

Actually, the hard terminate is the one I'm really after, I already call my cleanup procedure from the form.onClose handler in the main form of my application.  I guess I could place calls to it in several places so as to catch it on exception, but the hard terminate is a tough nut.  I don't want my program to appear responsible for screwing up somebody's desktop.... ya'know?
ASKER CERTIFIED SOLUTION
Avatar of MerijnB
MerijnB
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
private
procedure AppException(Sender: TObject; E: Exception);


-----


procedure TfrmMain.AppException(Sender: TObject; E: Exception);
var cla:string
begin
if Application.Terminated then begin
   if Sender <> NIL then cla:=Sender.ClassName;
   cla:=E.ClassName;
   MessageBox(frmMain.Handle, PAnsiChar('Error: '+br+Cla+br+E.Message),'Tatal Error', MB_ICONERROR or MB_OK or MB_TOPMOST or MB_SYSTEMMODAL) ;
   // do it what you want
ExitProcess(0) ;
end;
end;


procedure TfrmMain.FormCreate(Sender: TObject);
begin
Application.OnException:=AppException ;
end ;
your solution only handles exceptions, that's something different from getting terminated by the kernel.
To handle kernel you need Bill Gates help.
I figured that this would not be readily do able.  It would be a great place to put your cleanup if it really existed...... Eldorado....  I'm going to leave the question open for a while.  I'm sure I'm not the only one who has ever needed to do this.
The whole point of killing something with the task manager is because it's not responding anymore. In that case the kernel will do something which bypasses the application you kill...
what needs cleaning up ?
some programs do this at startup ...
actually the accepted items should be
#23056161

and a good suggestion is #23056199 but i think he took a step back ...
Dhaest,
do you have any practical Delphi experience ?