Just one instance of application

If you want to allow users to run just one instance of application, one of possible methods is to use “mutext” (mutual exclusion).

First of all, we need to edit DPR (project) file of our application. Before we initialize application and create main form, we try to create mutext of specific name. If no error occurs, we can continue to start application. But if “mutext” already exists, it means, that it was created previously by first instance of application. In this case, we just switch to this first instance and stop starting a new one.
The rest of the code is for handling messages and to bring “front” existing instance. Put it in the main form unit. In example below, project file (DPR) code and main form unit code are in the same “file”, but you must put it in the right separate files in your real application.

And don’t forget to add Windows unit to project file (DPR), because CreateMutex function is in this unit.