Click to See Complete Forum and Search --> : Mutex Problems...


Vodzurk
May 24th, 2006, 07:39 AM
Hi Guys,

I'm having a bizarre problem here with mutexes.

If I have the following code in a page (note that it never releases the mutex!), then load the page twice, in 2 seperate browsers (or simply hitting refresh), I do not get a mutex lock.

Any ideas? I'm trying to get a globally accessible mutex. Is using the Application[] object wrong somehow?

In Global.asax:

void Application_Start(object sender, EventArgs e)
{
Application["AppMutex"] = new System.Threading.Mutex();
}
In my page:

protected void Page_Load(object sender, EventArgs e)
{
if (!((System.Threading.Mutex)Application["AppMutex"]).WaitOne())
Label1.Text = "Cannot lock mutex";
else
Label1.Text = "Mutex locked ok";
}

wildfrog
May 27th, 2006, 12:33 AM
Any ideas? I'm trying to get a globally accessible mutex.You're using a local mutex (without a name), so basically you create a new mutex each time you load the page. What you want is to use a named mutex (or maybe it's called a named system mutex).

(note that it never releases the mutex!),No, no, no, you should always release a mutex before you close the thread. In this situation you've got no control over when the thread ends (AFAIK that is up to up to the ASP.NET host/IIS).

You should consider using a named system semaphore.

- petter

Vodzurk
May 28th, 2006, 05:08 PM
Mmm, am I missing something here? I'm pretty new to asp.net, but I thought objects put in the "Application" scope and initialised at the Application_Start level would be global to all sessions?

In my app, I have to cast it back, but it should still be the same object, right? Well, either way, it's not behaving as I expected.

I know a mutex should always be released... what I was trying to do here was to test if the lock was working by not releasing it.

wildfrog
May 28th, 2006, 09:42 PM
what I was trying to do here was to test if the lock was working by not releasing it.Yup, but you'll have to name your mutex. If you don't do that all your threads, processes or "application scopes" will get their own individual mutexes.

- petter