#1
|
|||
|
|||
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. |
#2
|
|||
|
|||
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)
|
#3
|
|||
|
|||
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 |
#4
|
|||
|
|||
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 |
#5
|
|||
|
|||
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 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 http://www.thescarms.com/dotNet/SingleInstance.asp Last edited by goggles99; 05-24-2005 at 16:47. |
#6
|
|||
|
|||
if you use MUTEXes as suggested by "just4urim", be sure to release your mutex using a call to releaseMutex() before your program quits
|
#7
|
|||
|
|||
Great! Thank you for all the help. I will report back once ive tested it
|
#8
|
|||
|
|||
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.
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
[C] NoDel (tool to prevent file deletion) | Insid3Code | Source Code | 0 | 02-10-2015 16:38 |
Prevent browser from being killed | nino | General Discussion | 4 | 01-10-2014 02:38 |