Home All Groups Group Topic Archive Search About
Author
26 Sep 2006 7:33 AM
cold80
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

Author
26 Sep 2006 6:07 PM
GhostInAK
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

Show quoteHide quote
> 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
>