I want to increment an integer that gets incremented in a timer event handler and read by the main and other worker threads i.e. one writer thread and multiple reader threads. Will it be thread-safe?
I have a timer in my application that runs every 5 seconds:
MyClock = new System.Threading.Timer(
new TimerCallback(this.Ticker), null, Timeout.Infinite, Timeout.Infinite );
that I turn on like this:
MyClock.Change(5000, 5000);
If I increment an integer in the Ticker
handler like this:
tickerCounter++;
can I then do a read-only access from main thread or worker threads of the same application? Will it be thread safe? Is there any chance of the reader to read a partial value, or causing a threading exception?