Here is the code:
private int _count = 0;
public bool Ongoing
{
get
{
return _count > 0;
}
}
public void Method1(object param)
{
new Thread(new ParameterizedThreadStart(Method2)).Start(param);
}
private void Method2(object param)
{
_count++;
lock (_lock)
{
// Something
}
_count--;
}
The variable _count, as you can guess, is used to count up how many threads are locked. It is initialized to 0 and only modified inside this method and I use it to know if the class is doing something.
Now the problem: sometimes _count goes below 0. It's like _count++ get sometimes ignored.
This happens very seldom, like about once every 500 times I start this method, maybe even less.
Should I declare _count as volatile maybe?