Critical section in WebForms

Discussion in 'ASP .Net' started by =?Utf-8?B?Q2hyaXN0aWFu?=, Oct 7, 2004.

  1. 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
    =?Utf-8?B?Q2hyaXN0aWFu?=, Oct 7, 2004
    #1
    1. Advertising

  2. =?Utf-8?B?Q2hyaXN0aWFu?=

    Scott Allen Guest

    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.

    --
    Scott
    http://www.OdeToCode.com/

    On Thu, 7 Oct 2004 08:07:06 -0700, Christian
    <> wrote:

    >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
    >
    Scott Allen, Oct 7, 2004
    #2
    1. Advertising

  3. 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))

    "Christian" wrote:

    > 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
    >
    >
    =?Utf-8?B?dG9tIHdpc25vd3NraQ==?=, Oct 8, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob Barrows
    Replies:
    0
    Views:
    392
    Bob Barrows
    Feb 27, 2004
  2. James Hunter Ross

    IISState output, hung on critical section?

    James Hunter Ross, Nov 8, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,832
    Bruce Barker
    Nov 8, 2005
  3. Dave Brueck
    Replies:
    2
    Views:
    468
    Skip Montanaro
    Sep 19, 2003
  4. aeromarine
    Replies:
    15
    Views:
    1,465
    Martin
    Feb 18, 2008
  5. Replies:
    0
    Views:
    159
Loading...

Share This Page