Actually there are several different ways to do this is .Net
For MIDI forms I usually us a private withevents variable that is local to the whole class and if it isn't nothing then I can't create a new form. You must handle the Closing or Closed event and set the variable to Nothing.
For other forms I use a simple bit of code.
#Region "PREVIOUS INSTANCE"
Public Function PREVIOUS_INSTANCE() As Boolean
If UBound(Diagnostics.Process.GetProcessesByName(Diag nostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
Return True
Else
Return False
End If
End Function
#End Region
OR YOU CAN USE A MUTEX
#Region "Declarations"
Private Shared _config As AppStartConfiguration = Nothing
Private Shared _exePath As String = ""
Private Shared _process As Process
Private Shared _appStartMutex As Mutex
Private Shared _mutexGuid As New Guid(New Byte() {&H5F, &H4D, &H69, &H63, &H68, &H61, &H65, &H6C, &H20, &H53, &H74, &H75, &H61, &H72, &H74, &H5F})
#End Region
#Region "Main"
<STAThread()> _
Shared Sub Main()
Dim isOwned As Boolean = False
_appStartMutex = New Mutex(True, _config.ExePath + _mutexGuid.ToString(), isOwned)
If Not isOwned Then
MessageBox.Show(String.Format(CultureInfo.CurrentC ulture, "There is already a copy of the application '{0}' running. Please close that application before starting a new one.", _config.ExePath))
Environment.Exit(1)
Exit Sub
End If
StartApp_Process()
End Sub
#End Region
It's important for us to explain to our nation that life is important. It's not only life of babies, but it's life of children living in, you know, the dark dungeons of the Internet."â George W. Bush - Arlington Heights, Ill., Oct. 24, 2000
|