Vladimír Klaus
Vladimír Klaus

Reputation: 157

How to wait for termination of itself

my currently running application (A1) needs to be terminated but as well as run some other application (A2). But I need to run application A2 after fully terminated of A1. Now I have something like this:

begin
  Application.Terminate;
  wait(2000); <<<<<<<
  ShellExecute(A2)...
end;

To be more exact - I need to call installation (A2) and want to be sure A1 is not running, because A2 is installation of A1. Please imagine that termination could last more time or it shows some modal dialog...

Is there any easy way how to do it (wait for it)? Of course without communication with or changing of A2! A2 could be anything else in the future.

Vladimír

Upvotes: 0

Views: 277

Answers (2)

SilverWarior
SilverWarior

Reputation: 8331

If want to make a proper installer/updater program don't worry about when and how do you execute it but instead how it will detect if your application is running or not.

Now if your main application already has a mechanizm to prevent starting of multiple instances of your application you already have half the work done. How?
Such mechanizms publish the information about an instance of your application already running to be available by other programs.

Most common appraoch to do so is by registering a named Mutex. So when second instance of your application starts it finds out that it can't create a new Mutex with the same name becouse one already exists. So now in most cases second instance of your application sends a custom message to the first instance to bring that instance to the front (restore application) and then closes itself.
If you want to read more about different mechanizms to controling how many instances of your application can be running at the same time I suggest you check the next article: http://delphi.about.com/od/windowsshellapi/l/aa100703a.htm

So how do you use such mechanizm for your installer/updater?
Just as you would check in second instance of your application to see if another instance is already running you check this in your installer/updater instead. You don't even need to do this schecking at the start of installer/updater. You can do it even later (downloading the update files first).
If there is an instance of your application running you broadcast a custom message. But this message is different from the one that one instance would send to another.
This will now tell your application that it is about to be updated so it should begin the closing procedure.
If you form this custom message in such a way that it also contains information about your installer/updater application.handle you give yourself the ability for your main application to send a return response in which it notifies the installer/updater in which state it is. For instnace:

  • asClosing (main application is just about to close)
  • asWaitinUserInput (main application is waiting for user to confirm save for instance)
  • asProcessing (main application is doing some lengthy processing so it can't shut down at this time)

And if there is no response in certain amount of time your installer could asume that your main application might be hung so it notifies the user that automatic closure of main application has failed and so that the user should close it manually and then retry the updating process.

Using such approach would allow you to start your installer/updater at any time during execution of your main application.
And not only that. You can start your installer/updater by double clicking its executable, by a shourtcut, by some other application or even by a windows task system.

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 612934

I need to call installation (A2) and want to be sure A1 is not running.

This is impossible. You cannot execute code in a process that has terminated. Once the process has terminated there is nothing that can execute code.

You'll need a new process. Start the new process with the sole task of waiting on its parent to terminate, and then do whatever is needed once the parent has terminated.

Upvotes: 3

Related Questions