I'm trying to check in my application if another instance of it is
already running. I found many code snippets on the net that make use of
a named mutex to do this check, but I can't make it work on visual
basic. Actually, it works sometimes and sometimes not. The code I'm
trying is:
Namespace WindowsApplicat ion2
Public Class Form1
Inherits Form
Public Sub New()
End Sub
Private Shared appGuid As String = "uniquekeyonmym achine"
Public Shared Sub Main()
Dim m As Mutex
m = New Mutex(False, appGuid)
If m.WaitOne(0, False) = False Then
MessageBox.Show ("Instance already running")
Return
End If
Application.Run (New Form1())
End Sub
End Class
End Namespace
Using this different piece of code in C# (and the keyword "using") the
method seems to work without problems (and always)
namespace WindowsApplicat ion1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeCompo nent();
}
static string appGuid = "uniquekeyonmym achine";
[STAThread]
static void Main()
{
using (Mutex mutex = new Mutex(false, appGuid))
{
if (!mutex.WaitOne (0, false))
{
MessageBox.Show ("Instance already running");
return;
}
Application.Run (new Form1());
}
}
}
}
What do you think can be the problem? Some people suggested that the
Garbage Collector could have destroy the mutex object before I tryied
to run another instance of the program, but the call to Application.Run
is a blocking one, isn't it? So the local object m should live until
the end of the program, right?
Thank you in advance for your help.
Cold
already running. I found many code snippets on the net that make use of
a named mutex to do this check, but I can't make it work on visual
basic. Actually, it works sometimes and sometimes not. The code I'm
trying is:
Namespace WindowsApplicat ion2
Public Class Form1
Inherits Form
Public Sub New()
End Sub
Private Shared appGuid As String = "uniquekeyonmym achine"
Public Shared Sub Main()
Dim m As Mutex
m = New Mutex(False, appGuid)
If m.WaitOne(0, False) = False Then
MessageBox.Show ("Instance already running")
Return
End If
Application.Run (New Form1())
End Sub
End Class
End Namespace
Using this different piece of code in C# (and the keyword "using") the
method seems to work without problems (and always)
namespace WindowsApplicat ion1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeCompo nent();
}
static string appGuid = "uniquekeyonmym achine";
[STAThread]
static void Main()
{
using (Mutex mutex = new Mutex(false, appGuid))
{
if (!mutex.WaitOne (0, false))
{
MessageBox.Show ("Instance already running");
return;
}
Application.Run (new Form1());
}
}
}
}
What do you think can be the problem? Some people suggested that the
Garbage Collector could have destroy the mutex object before I tryied
to run another instance of the program, but the call to Application.Run
is a blocking one, isn't it? So the local object m should live until
the end of the program, right?
Thank you in advance for your help.
Cold
Comment