By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,876 Members | 1,478 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,876 IT Pros & Developers. It's quick & easy.

Problem with mutex

P: n/a
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 WindowsApplication2
Public Class Form1
Inherits Form

Public Sub New()

End Sub

Private Shared appGuid As String = "uniquekeyonmymachine"

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 WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

static string appGuid = "uniquekeyonmymachine";

[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

Sep 26 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hello cold80,

This works fine:

Imports System
Imports System.Threading
Module Module1

Public Sub Main()

Dim tMutex As Mutex = New Mutex(False, "oggieboogieboo")
If Not tMutex.WaitOne(0, False) Then
MsgBox("This is a single-instance app, chedarhead.")
Return
End If

Application.Run(New Form1)
tMutex.ReleaseMutex()
tMutex = Nothing

End Sub

End Module

Also, be aware in VB you must set the startup form to "SUb Main()" in the
project properties. In VS05 this means unchecking the "Use Application Framework"
box first.

-Boo
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 WindowsApplication2
Public Class Form1
Inherits Form
Public Sub New()

End Sub

Private Shared appGuid As String = "uniquekeyonmymachine"

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 WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static string appGuid = "uniquekeyonmymachine";

[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

Sep 26 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.