Working through the details of the update mechanism.
This is one of more difficult parts of the application, even though on the surface it looks uncomplicated. If we run the installer and it detects that the app is already installed, it should just update the installation. How hard could it be?
If the program is not running, then it's indeed a simple matter of replacing the program file with a newer version. The fun starts if the program
On Windows, running a program locks its file, preventing any changes from being made to it. This is not required per se and there are operating systems that don't do this. There are also custom
loaders for Windows that can take an executable and create a running process out of it without any locking fuss. For example,
could do that back in 1999. See its source code for details if interested.
To complicate matters, there might be more than one process running off the same executable. For example, if several users are logged into a Windows box and each runs a copy of a program.
Therefore, the first order of business for any installer is to shut down all running instances of the program. A
installer should also restart all these instances after it's done with updating.
The shutting down part is not a big deal. There's more than one way to do this and all of them are fairly straight-forward.
It's the restarting that is contrived.
Not only program instances may be running under different user accounts, they may also be running with custom command-line arguments, as Administrators or system services. Restarting this cornucopia of awesomeness is not an envious task.
Here's how Bvckup works around this mess.
Every Bvckup executable has a corresponding named event in the Global namespace. The event is created by the first instance of the program when it's launched, and it is opened by all other instances.
The name of the event includes the
of the executable file. This groups running instances by the file they are using, because, after all, the installer is interested in a specific executable and nothing else.
For example, the following is an event name used by the Bvckup installation on my development box -
With this in place, the installer starts off by signaling the event.
In response, a running program makes a temporary copy of its executable file and starts it in a special "relauncher" mode. And then it exits.
The "relauncher" process opens the "update" event and simply spins there, waiting for it to become
Meanwhile the installer waits for the program file to become unlocked. Once all running instances replace themselves with the relaunchers, the executable frees up and the installer proceeds with the update. It then clears the "update" event and exits.
This releases the relaunchers, they restart program instances and -
- the update is completed.
Techincally, instead of adding a special "relauncher" mode to the program it's possible just to have a small standalone relauncher program. It comes down to a matter of taste. Some people don't like to spawn from temporary executables, and I like to keep my file count to a bare minimum. To each his own.