Beispiel #1
0
 private void OnPackTimer(object sender, MicroTimerEventArgs timerEventArgs)
 {
     //this.BeginInvoke(new MethodInvoker(delegate {
     //Debug.Print(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
     //}));
     checkPackData();
 }
        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;
                }
                if (!_stopTimer)
                {
                    MicroTimerEventArgs microTimerEventArgs =
                        new MicroTimerEventArgs(timerCount,
                                                elapsedMicroseconds,
                                                timerLateBy,
                                                callbackFunctionExecutionTime);
                    MicroTimerElapsed(this, microTimerEventArgs);
                }
            }

            microStopwatch.Stop();
        }