Problem with mutex

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cold80

    #1

    Problem with mutex

    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

  • GhostInAK

    #2
    Re: Problem with mutex

    Hello cold80,

    This works fine:

    Imports System
    Imports System.Threadin g
    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.ReleaseM utex()
    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 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

    Working...