Esempio n. 1
0
        void NotificationTimer(ref long timerIntervalInMicroSec,
                               ref long ignoreEventIfLateBy,
                               ref bool stopTimer)
        {
            try
            {
                int  timerCount       = 0;
                long nextNotification = 0;
                long lastNotification = 0;
                long delay            = 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);

                    lastNotification  = nextNotification;
                    nextNotification += timerIntervalInMicroSecCurrent;
                    timerCount++;
                    long elapsedMicroseconds = 0;

                    while ((elapsedMicroseconds = microStopwatch.ElapsedMicroseconds)
                           < nextNotification)
                    {
                        System.Threading.Thread.SpinWait(10);
                    }

                    long timerLateBy = elapsedMicroseconds - nextNotification;

                    if (timerLateBy >= ignoreEventIfLateByCurrent)
                    {
                        continue;
                    }

                    delay = microStopwatch.ElapsedMicroseconds - lastNotification;

                    MicroTimerEventArgs microTimerEventArgs =
                        new MicroTimerEventArgs(timerCount,
                                                elapsedMicroseconds,
                                                delay,
                                                timerLateBy,
                                                callbackFunctionExecutionTime);
                    MicroTimerElapsed(this, microTimerEventArgs);
                }

                microStopwatch.Stop();
            }
            catch (Exception e)
            {
                UnityEngine.Debug.LogError("Exception in thread : " + e + "\n");
            }
        }
Esempio n. 2
0
        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();
        }