How to create a Request Specific Thread Safe Static int Counter?

Posted by user960567 on Programmers See other posts from Programmers or by user960567
Published on 2012-12-10T12:49:28Z Indexed on 2012/12/10 17:29 UTC
Read the original article Hit count: 140

Filed under:
|
|

In one of my server application I have a class that look like,

class A
{
   static int _value = 0;
   void DoSomething()
   {
         // a request start here
          _value = 0;
         _value++;
         // a request end here
   }
   // This method can be called many time during request
   void SomeAsyncMethods()
   {
         _value++;
   }
}

The problem is SomeAsyncMethods is async. Can be called many times. What I need when a request start set _value = 0 and then asynchrosnously increment this. After end of request I need the total. But the problem is that another request at the same time can access the class.

© Programmers or respective owner

Related posts about c#

Related posts about ASP.NET