Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close and restart the current application in DELPHI

Tags:

How can I do this one?

For some reason or selected by the user, “ask” the current application to restart it self.

like image 396
DRokie Avatar asked May 16 '11 17:05

DRokie


2 Answers

uses ShellAPI;

...

procedure TForm1.RestartThisApp;
begin
  ShellExecute(Handle, nil, PChar(Application.ExeName), nil, nil, SW_SHOWNORMAL);
  Application.Terminate; // or, if this is the main form, simply Close;
end;
like image 144
Andreas Rejbrand Avatar answered Sep 19 '22 14:09

Andreas Rejbrand


There is another way for closing-restarting the application:

Save a scheduled task to a short time after the application closes. This will have to be the VERY LAST thing your application does before exiting (no further data processing, saving, uploading or whatever)

eg.

  1. get the system time first
  2. set the scheduled task some time after this time (scheduled event will have to start your executable)
  3. exit your application (closing the main form or application.terminate will do it)

When your program starts again, it should check for any such scheduled task and remove them. This must be the VERY FIRST action your application should do when starting. (cleanup)

  1. check for any scheduled tasks created by your executable
  2. remove them

AFAIK, The Delphi Jedi component set has a component you can do the task scheduling stuff with.

like image 37
beerwin Avatar answered Sep 20 '22 14:09

beerwin