Re: Mutex not working as expected

From: Jay B. Harlow [MVP - Outlook] (Jay_Harlow_MVP_at_msn.com)
Date: 02/16/05


Date: Tue, 15 Feb 2005 18:45:18 -0600

Tom,
> Mutex's are global to the system unless you specify the Local\\ prefix
> to the name... Are you sure that the mutex isn't getting GC'd? I came
Is this true?

Reading CreateMutex (I believe the underlying API used).

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createmutex.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/termserv/TermServ/kernel_object_namespaces.asp

I get the impression it is true on server side (Server OS) for server
software, however client side (XP Pro) its the opposite... & server side,
client software (as in a Terminal Server Session) its true...

I started looking at this, but did not make any progress:

I was using 2 accounts, one Admin & one User under Windows XP Pro SP2
(VS.NET 2003 .NET 1.1 SP1)

    Public Sub Main()
        Dim vBool As Boolean
        Dim FirstInstance As New Mutex(False, "Local\UniqueMutexName",
vBool)
        If Not vBool Then
            MessageBox.Show("Already running!", Application.ProductName,
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            MessageBox.Show("I'm first", Application.ProductName,
MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

When using "Local\UniqueMutexName" or "UniqueMutexName" shows "I'm first" in
both sessions.

While "Global\UniqueMutexName" causes an exception in the 2nd session (the
User session), The admin session (the first session worked). I suspect the
2nd session is failing because they don't have some Win32 priviledge.

I have not tried my Windows 2003 machine.

Unfortunately I have not got much further on researching this...

I was going to send a not to Jon Skeet, however I haven't tracked down his
email address yet...

Jay

"Tom Shelton" <tom@YOUKNOWTHEDRILLmtogden.com> wrote in message
news:%23aVHyT7EFHA.732@TK2MSFTNGP12.phx.gbl...
> In article <BE6C7EE7-3DD4-4056-90C0-A8B6D17E6DF4@microsoft.com>, mfdatsw1
> wrote:
>> I need to be certain my application only runs once on a machine, and I
>> implemented a solution using Mutex. The solution was posted many times,
>> but
>> one great link I found is
>> http://www.yoda.arachsys.com/csharp/faq/#one.application.instance
>>
>> The problem is, when I switch users (XP Professional) the second user can
>> open a second instance of the application! How can I prevent this?
>>
>> Here's the relevant part of my code:
>>
>> Sub Main()
>> Dim vBool As Boolean
>> Dim FirstInstance As New Mutex(False, "UniqueMutexName", vBool)
>> If Not vBool Then
>> Post message to user and exit.
>> End If
>>
>
> Mutex's are global to the system unless you specify the Local\\ prefix
> to the name... Are you sure that the mutex isn't getting GC'd? I came
> across this not to long ago. You should probably store the Mutex
> instance in a module level or shared variable to make sure that the
> reference is not collected during the life of the first instance...
>
> Private firstInstance As Mutext
>
> Public Shared Sub Main ()
> Dim first As Boolean
> firstInstance = New Mutex (....)
> If Not First Then
> ...
> Return
> End If
> End Sub
>
> Just a thought...
> --
> Tom Shelton [MVP]


Quantcast