GMSDev
asked on
Using a Mutex or Semaphore in ASP.NET Web Page
I have a website that is used internally in our company to processing of our workload. On the page is a button that a user can click to get the next item of work. When clicked the function queries the database for the next unassigned item.
We have seem some syncronization problems with this process where if two users request as the same time they get the same work item.
I need to create a critical section where only one request can get an assignment at a time. My first effort we looked at this from the database side. We queried the next document with an Update Lock and used ReadPast but this did not solve the assignment issue.
Normally, in a non-web based applicaiton, I would use a Mutex or Semaphore to manage the critical section, but I am not sure if that will work in ASP.NET and IIS. Can anyone tell me if this is the right approach or is there some other construct.
Thanks
We have seem some syncronization problems with this process where if two users request as the same time they get the same work item.
I need to create a critical section where only one request can get an assignment at a time. My first effort we looked at this from the database side. We queried the next document with an Update Lock and used ReadPast but this did not solve the assignment issue.
Normally, in a non-web based applicaiton, I would use a Mutex or Semaphore to manage the critical section, but I am not sure if that will work in ASP.NET and IIS. Can anyone tell me if this is the right approach or is there some other construct.
Thanks
You are on the right track. However, using a mutex/semaphore directly in the code-behind of a web page is typically frowned upon. My suggestion is to create a class with a few static properties that would wrap up all of the required data access and manages the state of the mutex/semaphore.
ASKER
I would assume then that we should create the semaphore as a static object and have it used by the methods you mentioned? Would that be a safe practice?
Thanks
Thanks
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
I used a non-static Mutex object in a static function that we already had for assigning the work items and that worked beautifully. The Mutex does not have to be static since it is designed to work accross processes already. If I had used a Semaphore it would have needed to be a static object but still works.
Thanks for the info.
Thanks for the info.