stat =>
{
TimerState s = (TimerState)stat;
- s.Counter++;
+ lock (s)
+ {
+ s.Counter++;
- s.TotalTicks += DateTimeOffset.UtcNow.Ticks - s.UtcNow.Ticks;
+ s.TotalTicks += DateTimeOffset.UtcNow.Ticks - s.UtcNow.Ticks;
- switch (s.Counter)
- {
- case 2:
- s.Period = 400;
- s.Timer.Change(TimeSpan.FromMilliseconds(s.Period), TimeSpan.FromMilliseconds(s.Period));
- break;
-
- case 4:
- s.TokenSource.Cancel();
- s.Timer.Dispose();
- break;
- }
+ switch (s.Counter)
+ {
+ case 2:
+ s.Period = 400;
+ s.Timer.Change(TimeSpan.FromMilliseconds(s.Period), TimeSpan.FromMilliseconds(s.Period));
+ break;
- s.UtcNow = DateTimeOffset.UtcNow;
+ case 4:
+ s.TokenSource.Cancel();
+ s.Timer.Dispose();
+ break;
+ }
+
+ s.UtcNow = DateTimeOffset.UtcNow;
+ }
},
state,
TimeSpan.FromMilliseconds(state.Period), TimeSpan.FromMilliseconds(state.Period));