Prevent more than one instance from any directory
After seeing a few examples submitted attempting to prevent more than one instance. I decided to submit my version. This code will only prevent the 2nd instance from running no matter what directory both .exe are executed from. In addition, it does not use App.PrevInstance and will not bring the 1st instance to the front, just merely a *WORKING* example of how to stop more then one instance from running and the code is very easy to understand and use in your own apps.
AI
AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Source Code
Private Function IsPrevInstance() As Boolean
Dim lngMutex As Long
lngMutex = CreateMutex(ByVal 0&, 1, App.Title)
If (Err.LastDllError = ERROR_ALREADY_EXISTS) Then
ReleaseMutex lngMutex
CloseHandle lngMutex
IsPrevInstance = True
Else
IsPrevInstance = False
End If
End Function
Private Sub Form_Load()
If IsPrevInstance = True Then
MsgBox "This Program Is Already Active!"
End
End If
End Sub
Original Comments (3)
Recovered from Wayback Machine