views:

152

answers:

2

I have 2 processes running. Both are creating a named mutex with the code

Mutex mut = new Mutex(false, "ABC");

then both the process use mut.WaitOne() and mut.ReleaseMutex() to access a shared database file, a number of times in different methods.

This code runs without any problems on XP.

On Vista, I just try to run one process (Process 1) and create the mutex using the same code. On the line mut.WaitOne(), Process 1 goes to sleep permanently. Notice, by this time I have not even started Process 2.

mut.WaitOne() is being called in a method (methodX). methodX is called in a loop from another method (methodY).

The structure of methodX is

try
{
    mut.WaitOne();
    //do work on the db file
}
catch
{
}
finally
{
    try
    {
        mut.ReleaseMutex();
    }
    catch
    {
    }
}

Also methodY and methodX are both working in a BackgroundWorker. While the code to initialize mutex was called in the main application. But, main application never owns the mutex.


Edit

Here is the code:

string mutexName = @"Global\OneComClientDb";
bool mutexWasCreated;
MutexSecurity mSec = new MutexSecurity();
MutexAccessRule rule = new MutexAccessRule(
                         new SecurityIdentifier(WellKnownSidType.WorldSid, null),
                         MutexRights.FullControl,
                         AccessControlType.Allow);
mSec.AddAccessRule(rule);
mut = new Mutex(false, mutexName, out mutexWasCreated, mSec);
A: 

Are the two processes running in different sessions? Have you tried:

Mutex mut = new Mutex(false, @"Global\ABC" );
Philip Davis
yes i tried that but still the same result .. infact i tried this code..string mutexName = @"Global\ABC"; bool mutexWasCreated; MutexSecurity mSec = new MutexSecurity(); MutexAccessRule rule = new MutexAccessRule( new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow); mSec.AddAccessRule(rule); mut = new Mutex(false, mutexName, out mutexWasCreated, mSec);
Aamir
Is it possible that something else has that mutex open? Maybe try changing the mutex name or rebooting the computer...
Philip Davis
A: 
string mutexName = @"Global\OneComClientDb";
bool mutexWasCreated;
MutexSecurity mSec = new MutexSecurity();
MutexAccessRule rule = new MutexAccessRule(
      new SecurityIdentifier(WellKnownSidType.WorldSid, null),
      MutexRights.FullControl, AccessControlType.Allow);
mSec.AddAccessRule(rule);
mut = new Mutex(false, mutexName, out mutexWasCreated, mSec);

here is the code

Aamir