Exetools

Exetools (https://forum.exetools.com/index.php)
-   General Discussion (https://forum.exetools.com/forumdisplay.php?f=2)
-   -   How to?-Prevent multiple instance of an application (https://forum.exetools.com/showthread.php?t=7593)

SOLAR 05-24-2005 10:15

How to?-Prevent multiple instance of an application
 
I am writin the app in vb 6.0 and vb.net and want to know how to prevent the application from being opened multiple times on the same computer.

i.e. If the application is already open clicking on the app's icon will only bring the currently open application to the front instead of opening another instance of the app.

Thank you for all help given.

just4urim 05-24-2005 12:26

One of the best and easiest way to prevent ur app. from running again when its run , is to create a named Mutex for ur app. then you should check it at the begining of ur app. and if CreateMutex returns the ERROR_ALREADY_EXISTS , then you can be sure that another instance of ur app is running and do whtevere u want ! (Find more info about the "CreateMutex" API in MSDN) :rolleyes:

oVERfLOW 05-24-2005 14:11

I prefer to use FindWindowEx API to find a similar window
You can find your window properties by SPY++

I think that it's more useful because your process may be killed and the user won't be able to launch your app by the reboot.

ba bye

surferxyz 05-24-2005 15:22

VB.Net

Code:

            '#Zone " Prevent multiple instances "
            If UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
                'Send opening form's TEXT property as a parameter to the function "ActivatePrevInstance"
                Dim PrevHndl As Long
                Dim result As Long
                Dim argStrAppToFind As String = "XXXXXXXXXXXXXXXX"
                'Variable to hold individual Process
                Dim objProcess As New Process
                'Collection of all the Processes running on local machine
                Dim objProcesses() As Process
                ''Get all processes into the collection
                objProcesses = Process.GetProcesses()

                For Each objProcess In objProcesses
                    ''Check and exit if we have SMS running already
                    If objProcess.MainWindowTitle.Length > 9 Or objProcess.MainWindowTitle = "xxxxxxxxxxxxxxxxxxxx" Then

                        If objProcess.MainWindowTitle = "xxxxxxxxxxxxxxxxxxxxxx" Then
                            MsgBox("Another instance of " & argStrAppToFind & " is already running on this machine. You cannot run TWO instances at a time. Please use the other instance.", MsgBoxStyle.Exclamation)
                            PrevHndl = objProcess.MainWindowHandle.ToInt32()
                            Exit For
                        End If
                        If (UCase(objProcess.MainWindowTitle.Substring(0, 12)) = UCase(argStrAppToFind)) Then
                            MsgBox("Another instance of " & argStrAppToFind & " is already running on this machine. You cannot run TWO instances at a time. Please use the other instance.", MsgBoxStyle.Exclamation)
                            PrevHndl = objProcess.MainWindowHandle.ToInt32()
                            Exit For
                        End If

                    End If
                Next
                If PrevHndl = 0 Then Exit Sub 'if No previous instance found exit the application.
                ''If found
                result = OpenIcon(PrevHndl) 'Restore the program.
                result = SetForegroundWindow(PrevHndl) 'Activate the application.

                End 'End the current instance of the application.
            End If
            '#End Zone


goggles99 05-24-2005 16:44

hmm, for vb6 it can be done very simply using "App.PrevInstance".

Code:

Private Form_Load( )
    If App.PrevInstance Then
          Unload Me
    End If
End Sub

for vb.net it's a bit different, but this function should work
Code:

Function PrevInstance() As Boolean
If Ubound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess).ProcessName)) > 0 Then
    Return True
Else
    Return False 
End If
End Function

There is also a similar way to do it in vb.net here
http://www.thescarms.com/dotNet/SingleInstance.asp

:D

redbull 05-24-2005 22:39

if you use MUTEXes as suggested by "just4urim", be sure to release your mutex using a call to releaseMutex() before your program quits

SOLAR 05-24-2005 23:37

Great! Thank you for all the help. I will report back once ive tested it

tAz 05-25-2005 01:58

in addition, you'll need to find the previously running app (findwindow), and make it the active window before you exit your program (use win messages). this way, running a 2nd instance of your app brings up the first instance.


All times are GMT +8. The time now is 18:39.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2026, vBulletin Solutions, Inc.
Always Your Best Friend: Aaron, JMI, ahmadmansoor, ZeNiX