Help on Asp.Net Code Syncronization

Discussion in 'ASP .Net' started by Roberto Rasto, Sep 30, 2009.

  1. I have a class method that can be executed by only one request at once.
    For example if 2 user ask for a page that call this class method, the second
    user must wait for the page of the first user end the execution.

    This is because this method execute some code that can't be execute twice at
    the same time.

    I remember that in visual C there was a lock statement (or similar) that
    prevent one code block to be execute twice.

    How can I do it in asp.net (I use vb)?

    Thanks a lot.

    Roberto
    Roberto Rasto, Sep 30, 2009
    #1
    1. Advertising

  2. Roberto Rasto

    Chris Taylor Guest

    Hi,

    Since each ASP.NET request is executed on a separate thread, you can take
    advantage of the .NET thread synchronization support.

    For synchronization within the same App Domain (to put it simplistically)
    take a look at System.Threading.Monitor class or the syntactic sugar for
    this in C# is lock and SyncLock in VB.NET
    http://msdn.microsoft.com/en-us/library/system.threading.monitor.aspx

    For cross process/app domain synchronization you can look towards the slower
    but more global System.Threading.Mutex class.

    Depending what you class does you need to consider if the synchronization is
    required across a web garden ie. multiple instances of the ASP.NET worker
    process executing the same application. For example if the class writes to a
    system wide shared resource like a file you might need to use the Mutex. If
    the class requires purely per instance sync then Monitor/lock/SyncLock
    should do the trick.

    Hope I have not over complicated your question...

    Best Regards,

    --
    Chris Taylor
    http://taylorza.blogspot.com
    http://twitter.com/taylorza


    "Roberto Rasto" <> wrote in message
    news:...
    > I have a class method that can be executed by only one request at once.
    > For example if 2 user ask for a page that call this class method, the
    > second user must wait for the page of the first user end the execution.
    >
    > This is because this method execute some code that can't be execute twice
    > at the same time.
    >
    > I remember that in visual C there was a lock statement (or similar) that
    > prevent one code block to be execute twice.
    >
    > How can I do it in asp.net (I use vb)?
    >
    > Thanks a lot.
    >
    > Roberto
    Chris Taylor, Oct 1, 2009
    #2
    1. Advertising

  3. Roberto Rasto

    Patrice Guest

    See the "SyncLock" keyword but unless this is something you have done
    yourself to get this behavior, you may want to give some more details (i.e
    if this is to workaround a behavior you don't want, some more details could
    help to remove this behavior rather than using a workaround).


    --
    Patrice

    "Roberto Rasto" <> a écrit dans le message de groupe
    de discussion : ...
    > I have a class method that can be executed by only one request at once.
    > For example if 2 user ask for a page that call this class method, the
    > second user must wait for the page of the first user end the execution.
    >
    > This is because this method execute some code that can't be execute twice
    > at the same time.
    >
    > I remember that in visual C there was a lock statement (or similar) that
    > prevent one code block to be execute twice.
    >
    > How can I do it in asp.net (I use vb)?
    >
    > Thanks a lot.
    >
    > Roberto
    >
    Patrice, Oct 1, 2009
    #3
  4. Thanks a Lot.
    I used SyncLock that works perfectly.


    "Chris Taylor" <> ha scritto nel messaggio
    news:...
    > Hi,
    >
    > Since each ASP.NET request is executed on a separate thread, you can take
    > advantage of the .NET thread synchronization support.
    >
    > For synchronization within the same App Domain (to put it simplistically)
    > take a look at System.Threading.Monitor class or the syntactic sugar for
    > this in C# is lock and SyncLock in VB.NET
    > http://msdn.microsoft.com/en-us/library/system.threading.monitor.aspx
    >
    > For cross process/app domain synchronization you can look towards the
    > slower but more global System.Threading.Mutex class.
    >
    > Depending what you class does you need to consider if the synchronization
    > is required across a web garden ie. multiple instances of the ASP.NET
    > worker process executing the same application. For example if the class
    > writes to a system wide shared resource like a file you might need to use
    > the Mutex. If the class requires purely per instance sync then
    > Monitor/lock/SyncLock should do the trick.
    >
    > Hope I have not over complicated your question...
    >
    > Best Regards,
    >
    > --
    > Chris Taylor
    > http://taylorza.blogspot.com
    > http://twitter.com/taylorza
    >
    >
    > "Roberto Rasto" <> wrote in message
    > news:...
    >> I have a class method that can be executed by only one request at once.
    >> For example if 2 user ask for a page that call this class method, the
    >> second user must wait for the page of the first user end the execution.
    >>
    >> This is because this method execute some code that can't be execute twice
    >> at the same time.
    >>
    >> I remember that in visual C there was a lock statement (or similar) that
    >> prevent one code block to be execute twice.
    >>
    >> How can I do it in asp.net (I use vb)?
    >>
    >> Thanks a lot.
    >>
    >> Roberto

    >
    Roberto Rasto, Oct 1, 2009
    #4
  5. Roberto Rasto

    Patrice Guest

    Would't want to insist but having to create a page that needs to run in an
    exclusive fashion is really unusual. Unless you are 100 % sure of your
    design you may want to post some details about why you need to do that (this
    is to make sure synclock is not used to workaround the symptoms when you
    could perhaps cure the root cause)...

    --
    Patrice
    Patrice, Oct 2, 2009
    #5
    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. dave
    Replies:
    0
    Views:
    1,256
  2. Peri
    Replies:
    2
    Views:
    2,927
  3. Karsten Wutzke
    Replies:
    3
    Views:
    527
    Karsten Wutzke
    Apr 21, 2004
  4. mtp
    Replies:
    11
    Views:
    648
    Robert Klemme
    May 10, 2006
  5. red floyd

    Re: syncronization

    red floyd, Jan 31, 2009, in forum: C++
    Replies:
    1
    Views:
    506
Loading...

Share This Page