How to run a single instance of an application (part 4 of 4)
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 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.
This article is copyright © Peter Johnson 2004-2013
Licensed under a Creative Commons License.