Module Global
Declare Function ShowWindow Lib "user32" (ByVal hWnd As IntPtr, ByVal nCmdShow As Long) As Long
Dim MyMutex As Threading.Mutex
Public Sub main()
'declare constants
Const SW_MAXIMIZE = 3
Const SW_SHOWMINIMIZED = 2
Const SW_RESTORE = 9
Const SHOWNORMAL = 1
Const SW_SHOW = 5
'Evaluate if this application is already running on this computer
Dim NoPrevInstance As Boolean
'I changed the string here to make it more obvious that it needs to be changed when this code is used:
MyMutex = New Threading.Mutex(True, "Some String that's specific to your app", NoPrevInstance)
If Not NoPrevInstance Then
'if this application is already running, show it
Dim processes() As Diagnostics.Process = Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)
If processes.Length > 1 Then
Dim mProcess As Diagnostics.Process
For Each p As Diagnostics.Process In processes
If mProcess Is Nothing Then
mProcess = p
Else
If p.StartTime < mProcess.StartTime Then mProcess = p
End If
Next
Dim hwnd As IntPtr = mProcess.MainWindowHandle
'Why did you have these two lines in here:
'Call ShowWindow(hwnd, SW_SHOWMINIMIZED)
'Call ShowWindow(hwnd, SHOWNORMAL)
Call ShowWindow(hwnd, SW_SHOW)
Exit Sub
End If
End If
Application.EnableVisualStyles()
Application.DoEvents()
Application.Run(New StartForm)
End Sub
End Module