Critical section in WebForms

G

Guest

I have a class named AccessClass to access a legacy system.
In a webform i instanciate an AccessClass object named myAccess
and i call a method : myAccess.accessmethod()

I want to insure that no two webforms run simultaneouslly the code in
accessMethod()

So the code of accessMethode() is

void accessMethod()
{
lock(this)
{
... code ...
}
}

My questions are :
Is this insure my goal ? Is my code a critical section for all clients ?
I have a doubt because each webform has his proper instance of AccessClass.
If two webforms instanciate an AccessClass object and call AccessMethod() at
the same time, there is two different objects so if the critical section is
relative to the object there is no critical section among all clients.

If my solution is bad, an other idea is to pass a reference to the
"Application" object to myMethod and to use it as the lock parameter. Indeed
"Application" is shared between all clients.
Is it a better idea ?

If not, what is the solution ?

Thanks,

Christian
 
S

Scott Allen

Hi Christian:

The way you describe the code then no, you are not getting exclusive
access to the system becauseeach client is locking on a different
instance.

You want the locks to be as private as possible. Locking on the
Application object can cause all sorts of problems, because anyone can
lock on the Application object.

My suggestion would be to add a private static object field to the
AccessClass and lock on that object. It's visible only inside the
AccessClass but being static, every client will lock on the same
object instance.

Just be very careful and test well, I'm sure you know additional
locks, particularly in long running calls can kill a web application.
 
G

Guest

You are using two different locks to lock the section therefor each thread
will be able to access the section. The reason for this is when each form
instantiates the AccessClass, they get their own instance of the obj. They
will then lock on thier own instance, defeating the purpose.
USE: lock(typeof(AccessClass))
 

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

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top