private void NotificationTimer(ref long timerIntervalInMicroSec, ref long ignoreEventIfLateBy, ref bool stopTimer) { var timerCount = 0; long nextNotification = 0; var microStopwatch = new MicroStopwatch(); microStopwatch.Start(); while (!stopTimer) { var callbackFunctionExecutionTime = microStopwatch.ElapsedMicroseconds - nextNotification; var timerIntervalInMicroSecCurrent = Interlocked.Read(ref timerIntervalInMicroSec); var ignoreEventIfLateByCurrent = Interlocked.Read(ref ignoreEventIfLateBy); nextNotification += timerIntervalInMicroSecCurrent; timerCount++; long elapsedMicroseconds; while ((elapsedMicroseconds = microStopwatch.ElapsedMicroseconds) < nextNotification) { Thread.SpinWait(10); } var timerLateBy = elapsedMicroseconds - nextNotification; if (timerLateBy >= ignoreEventIfLateByCurrent) { continue; } var microTimerEventArgs = new MicroTimerEventArgs(timerCount, elapsedMicroseconds, timerLateBy, callbackFunctionExecutionTime); this.MicroTimerElapsed(this, microTimerEventArgs); } microStopwatch.Stop(); }
private void NotificationTimer(ref long timerIntervalInMicroSec, ref long ignoreEventIfLateBy, ref bool stopTimer) { var timerCount = 0; long nextNotification = 0; var microStopwatch = new MicroStopwatch(); microStopwatch.Start(); while (!stopTimer) { var callbackFunctionExecutionTime = microStopwatch.ElapsedMicroseconds - nextNotification; var timerIntervalInMicroSecCurrent = Interlocked.Read(ref timerIntervalInMicroSec); var ignoreEventIfLateByCurrent = Interlocked.Read(ref ignoreEventIfLateBy); nextNotification += timerIntervalInMicroSecCurrent; timerCount++; long elapsedMicroseconds; while ((elapsedMicroseconds = microStopwatch.ElapsedMicroseconds) < nextNotification) { Thread.SpinWait(10); } var timerLateBy = elapsedMicroseconds - nextNotification; if (timerLateBy >= ignoreEventIfLateByCurrent) { continue; } var microTimerEventArgs = new MicroTimerEventArgs(timerCount, elapsedMicroseconds, timerLateBy, callbackFunctionExecutionTime); MicroTimerElapsed(this, microTimerEventArgs); } microStopwatch.Stop(); }