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