Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


ID: 20735, How to prevent multiple application instances?

by Sergey Kryukov Email: Anonymous


There are many solutions published. This is the only complete one, as far as I know.
Download Details
FTP  download also available
CDN Login Required to Download. (You will be redirected to the login page if you click on the Download Link)
To download this, you must have registered:
A free membership

For C#Builder, Version 1.0  to 1.0 625 downloads
Copyright: No significant restrictions


Size: 5,534 bytes
Updated on Tue, 07 Oct 2003 09:09:40 GMT
Originally uploaded on Tue, 07 Oct 2003 09:03:32 GMT
SHA1 Hash: 1BAC321D353985591257A8092038BB4F9E446E25
MD5 Hash: 7CB5404C86AF03D919EA6DE2F2F7D125

    Explore the files in this upload

Description
There are many solutions published. This is the only complete one, as far as I know.

One should take into account the following: processes can be found by its executable file name, without path and extension, but for comparison of the current and previous instance must involve full path. From the other hand, comparison of the current instance with all the available processes would be more costly. Finally, to exclude the current process from the search it Id can be used.

Quite another problem is to bring the previous instance to front if the application is windowed. Just making the main window found in the previous instance a foreground one would not always work because of modal forms or dialog possibly running. See my solution for more detail.

Solution:

See the cSingletonApp class (SingletonApp.cs).

cSingletonApp.PreviousInstanceDetected detects previous instance of the application and returns its MainWindowHandle (if any).

cSingletonApp.ShowPreviousInstance is more tricky. If the MainWindowHandle of the previous application instance is available, it activates the main window. What makes it tricky is that it works correctly even if some modal form is active or some dialog is running.

How to build demo?

See demo application DemoApplication.cs. The calls to the cSingletonApp methods are shown in the region "Demo code".

You only need the .NET framework installed. Assuming there is a framework's bin directory in your PATH, 1) run build.bat, 2) or run make or nmake in the directory where the makefile is located.

-- Sergey A. Kryukov

http://www.SAKryukov.org

For more information, see http://www.SAKryukov.org

   Latest Comments  View All Add New

Move mouse over comment to see the full text

Could not retrieve comments. Please try again later.

Server Response from: ETNACDC03