How to run a single instance of an application (part 4 of 4)

Demo code, conclusion and feedback

Demonstration code

Real life example
For an example of a real life application that uses the techniques presented here, see the source code of my Version Information Spy program.

Demo projects including the source code from this article can be downloaded. The download includes the following:

  • The initial template demo project.
  • The USingleInst unit along with a demo project that shows how to override TSingleInst and how to modify the main form and project files.
  • A read-me file explaining how to use the code.

The code has been tested with Delphi 7 and Delphi 2010. It is known not to work with Delphi 5. Delphi 6 may work, but this has not been tested.

This demo code is merely a proof of concept and is intended only to illustrate this article. It is not designed for use in its current form in finished applications. The code is provided on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
The demo is open-source. See any license information included in and with the source files for details.


This article has demonstrated how to ensure that only a single instance of an application is run. We have also shown how to pass command line parameters to an already running instance, ensuring its window is displayed prominently.

We detected previous instances by searching for the application's main window class among the top level windows. Communication with a previous instance was by means of messages sent to the instance's main window. In particular, a user defined message was used to ensure the window was restored and visible and the WM_COPYDATA message was used to send the command line parameters across the process boundary.

Skeletal source code for an application that runs as a single instance was developed. A reusable class was then presented that can be used in developing such a program.

Demonstration code incorporating the source code developed in the article was made available.


I hope you enjoyed this article and found it useful. If you have any observations, comments or have found any errors please contact me.