Your code is a lot like what I am using, but I noticed you are not releasing
your mutex. Not even sure of the ramifications of that, but here is what I
use...
HTH,
Greg
' code to start app...
Try
SingletonApp.Run(New MyMainForm, "8ca35a66-6e9a-41d4-a87d-d9755b1f88c4") '
arbitrary GUID
Catch ex As SingletonException
MsgBox("Can not start because a previous instance of this application is
already running!")
End Try
' code inside singletonapp.vb file
Option Strict On
Imports System.Threading
Public Class SingletonApp
Private Shared _guid As String
Shared m_Mutex As Mutex
Public Shared Sub Run(ByVal mainForm As Form, ByVal guid As String)
_guid = guid
If (IsFirstInstance()) Then
AddHandler Application.ApplicationExit, AddressOf OnExit
Application.Run(mainForm)
Else
Throw New SingletonException
End If
End Sub
Public Shared Function IsFirstInstance() As Boolean
m_Mutex = New Mutex(False, _guid)
Dim owned As Boolean = False
owned = m_Mutex.WaitOne(TimeSpan.Zero, False)
Return owned
End Function
Public Shared Sub OnExit(ByVal sender As Object, ByVal args As
EventArgs)
m_Mutex.ReleaseMutex()
m_Mutex.Close()
End Sub
End Class
Public Class SingletonException
Inherits System.ApplicationException
Public Sub New()
MyBase.New("Program already running!")
End Sub
Public Sub New(ByVal InnerException As Exception)
MyBase.New("Program already running!", InnerException)
End Sub
End Class
"JZ" <jj@anon.anon.com> wrote in message
news:41***********************@news.dial.pipex.com ...
Hi,
I'm not sure whether it is XP SP2, it might just be my PC.
However, just in case anyone else is reading this, I found a work around
for what I needed to do...
Dim appMutex As New System.Threading.Mutex(False, "MyApplicationName")
If appMutex.WaitOne(0, False) Then
'Application.Run(New MyMainForm())
Else
MessageBox.Show _
("Can not start because a previous instance of this application is
already running.")
End If
--
JZ