the code looks like this:
public class JobManager
{
public static void TrackExceptionCount(ref int exceptionCount)
{
Interlocked.Increment(ref exceptionCount);
}
//other helper methods
}
I will call this method in other place like:
private static int _exceptionCount;
JobManager.TrackExceptionCount(ref _exceptionCount);
I thought Interlocked.Increment will handle the thread-safe problem, right?
Edit:
I have multiple Job class like this:
class JobA
{
private static int _exceptionCount;
public void method1()
{
Task.Factory.Start(()=>{
try
{
//Some code
}
catch(exception ex)
{
JobManager.TrackExceptionCount(ref _exceptionCount);
}
});
}
public void method2()
{
Task.Factory.Start(()=>{
try
{
//Some code
}
catch(exception ex)
{
JobManager.TrackExceptionCount(ref _exceptionCount);
}
});
}
}
class JobB
{
private static int _exceptionCount;
public void method1()
{
Task.Factory.Start(()=>{
try
{
//Some code
}
catch(exception ex)
{
JobManager.TrackExceptionCount(ref _exceptionCount);
}
});
}
public void method2()
{
Task.Factory.Start(()=>{
try
{
//Some code
}
catch(exception ex)
{
JobManager.TrackExceptionCount(ref _exceptionCount);
}
});
}
}
I believe that direct call Interlocked.Increment in the catch block probably a better way.
But still want to know if the JobManager.TrackExceptionCount(ref _exceptionCount) will be working probably, or not
Thanks.