/// <summary> /// Micro Timer Event /// </summary> /// <param name="sender"></param> /// <param name="timerEventArgs"></param> private void OnTimedEvent(object sender, SickLidar.MicroTimerEventArgs timerEventArgs) { if (this.is_timer_interval == false) { this.panaLidar.before_timer = timerEventArgs.ElapsedMicroseconds; this.is_timer_interval = true; } else { this.panaLidar.timer_time = (double)(timerEventArgs.ElapsedMicroseconds - this.panaLidar.before_timer) / (double)(1000.0); this.panaLidar.before_timer = timerEventArgs.ElapsedMicroseconds; } // sensor count this.panaLidar.sensor_count = timerEventArgs.TimerCount; // current time this.panaLidar.current_time = this.MillisecondDisplay(); // stopwatch start this.hTimer.Start(); // sensor data acquisition List <string> gd1_message = this.panaLidar.GD(this.panaLidar.set_parameter.amin, this.panaLidar.set_parameter.amax, '1'); // decoding received data this.panaLidar.Decode(gd1_message); // save mode if (this.panaLidar.file_mode == 2) { this.writer.WriteRow(this.panaLidar.save_data); } // convert decoded data to 3d point cloud this.panaLidar.DecodedDataToPointCloud(); // draw data if (this.panaLidar.point_cloud.Count != 0) { this.UpdateGraph(); } // stopwatch end this.hTimer.Stop(); // processing time this.panaLidar.elapsed_time = this.hTimer.Duration; //// Do something small that takes significantly less time than Interval //Console.WriteLine(string.Format( // "Count = {0:#,0} Timer = {1:#,0} µs, " + // "LateBy = {2:#,0} µs, ExecutionTime = {3:#,0} µs", // timerEventArgs.TimerCount, timerEventArgs.ElapsedMicroseconds, // timerEventArgs.TimerLateBy, timerEventArgs.CallbackFunctionExecutionTime)); }
void NotificationTimer(ref long timerIntervalInMicroSec, ref long ignoreEventIfLateBy, ref bool stopTimer) { int timerCount = 0; long nextNotification = 0; MicroStopwatch microStopwatch = new MicroStopwatch(); microStopwatch.Start(); while (!stopTimer) { long callbackFunctionExecutionTime = microStopwatch.ElapsedMicroseconds - nextNotification; long timerIntervalInMicroSecCurrent = System.Threading.Interlocked.Read(ref timerIntervalInMicroSec); long ignoreEventIfLateByCurrent = System.Threading.Interlocked.Read(ref ignoreEventIfLateBy); nextNotification += timerIntervalInMicroSecCurrent; timerCount++; long elapsedMicroseconds = 0; while ((elapsedMicroseconds = microStopwatch.ElapsedMicroseconds) < nextNotification) { System.Threading.Thread.SpinWait(10); } long timerLateBy = elapsedMicroseconds - nextNotification; if (timerLateBy >= ignoreEventIfLateByCurrent) { continue; } MicroTimerEventArgs microTimerEventArgs = new MicroTimerEventArgs(timerCount, elapsedMicroseconds, timerLateBy, callbackFunctionExecutionTime); MicroTimerElapsed(this, microTimerEventArgs); } microStopwatch.Stop(); }