Help on Asp.Net Code Syncronization

  • Thread starter Thread starter Roberto Rasto
  • Start date Start date
R

Roberto Rasto

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
 
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,
 
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).
 
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)...
 
Back
Top