Note that there are some explanatory texts on larger screens.

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

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload