Easy mutex problem...

G

google

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, 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";
}
 
A

Alvin Bruney

What are you doing with the mutex? Why do you need one that is global but is
being accessed on a per page basis?

--

________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
 
V

Vodzurk

I was trying to control access to a resource at an application level,
not per-page-level or session-level.
 
A

Alvin Bruney

I figured that was what you were after. I mutex is really overkill for that
situation. Just lock the resource. As to the original issue, I'm not sure
why it is happening. Maybe if you could scratch up some code to reproduce
the issue, we could look at it.

lock(resource)
{
}

--

________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
 
A

Alvin Bruney

You also need to explicitly release the mutex object by the way which is why
the mutex isn't being released. Since it is a kernel object, it isn't scoped
to the block of code it is running in.

--

________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top