예제 #1
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;

                // https://www.codeproject.com/Articles/98346/Microsecond-and-Millisecond-NET-Timer?msg=5288003#xx5288003xx
                //while ( (elapsedMicroseconds = microStopwatch.ElapsedMicroseconds)
                //        < nextNotification)
                //{
                //    System.Threading.Thread.SpinWait(10);
                //}
                int wms = (int)timerIntervalInMicroSec / 1000;
                wms = wms - (int)(wms * 0.01);
                _threadTimer.Join(wms);
                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();
        }
        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);
                    System.Threading.Thread.Sleep(1);
                }

                long timerLateBy = elapsedMicroseconds - nextNotification;

                //if (timerLateBy >= ignoreEventIfLateByCurrent)
                //{
                //    nextNotification += timerIntervalInMicroSecCurrent;
                //}
                //else
                {
                    MicroTimerEventArgs microTimerEventArgs =
                        new MicroTimerEventArgs(timerCount,
                                                elapsedMicroseconds,
                                                timerLateBy,
                                                callbackFunctionExecutionTime);
                    MicroTimerElapsed(this, microTimerEventArgs);
                }
            }

            microStopwatch.Stop();
        }
예제 #3
0
 private void OnTimedEvent(object sender,
                           MicroLibrary.MicroTimerEventArgs timerEventArgs)
 {
     // Do something small that takes significantly less time than Interval.
     // BeginInvoke executes on the UI thread but this calling thread does not
     //  wait for completion before continuing (i.e. it executes asynchronously)
     if (InvokeRequired)
     {
         BeginInvoke((MethodInvoker) delegate
         {
             LogfData();
         });
     }
 }
예제 #4
0
 private void OnTimedEvent(object sender, MicroLibrary.MicroTimerEventArgs timerEventArgs)
 {
     Callback();
     if (singleTick)
     {
         Stop();
     }
     //// 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));
 }
예제 #5
0
        private void microTimerEvent(object sender, MicroLibrary.MicroTimerEventArgs timerEventArgs)
        {
            if (_port.IsOpen)
            {
                try
                {
                    switch (_state)
                    {
                    case CommunicationState.Start:
                        handle5BaudTimer();
                        break;

                    case CommunicationState.SendCommand:
                        //_stopWatch.Stop();
                        //TimeSpan ts = _stopWatch.Elapsed;
                        //Console.WriteLine("byte interval was " + ts.TotalMilliseconds + " ms");
                        //_stopWatch.Reset();
                        //_stopWatch.Start();

                        byte[] b = new byte[1];
                        b[0] = _sendMsg[_sendctr];
                        ++_echo;     // ignore the echo byte that will be coming
                        _port.Write(b, 0, 1);


                        ++_sendctr;
                        if (_sendctr >= _sendMsg.Count)
                        {
                            //_sendMsg.Clear();
                            _timeout            = 0;
                            _state              = CommunicationState.WaitForResponse;
                            _sendctr            = 0;
                            _microTimer.Enabled = false;
                            Console.WriteLine("Finished sending");
                        }

                        break;
                    }
                }
                catch (Exception E)
                {
                    //AddToLog(E.Message);
                    Console.WriteLine(E.Message);
                }
            }
        }
        void NotificationTimer(long timerInterval,
                               long ignoreEventIfLateBy,
                               ref bool stopTimer)
        {
            int  timerCount       = 0;
            long nextNotification = 0;

            MicroStopwatch microStopwatch = new MicroStopwatch();

            microStopwatch.Start();

            while (!stopTimer)
            {
                long callbackFunctionExecutionTime =
                    microStopwatch.ElapsedMicroseconds - nextNotification;
                nextNotification += timerInterval;
                timerCount++;
                long elapsedMicroseconds = 0;

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

                long timerLateBy = elapsedMicroseconds - (timerCount * timerInterval);

                if (timerLateBy >= ignoreEventIfLateBy)
                {
                    continue;
                }

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

            microStopwatch.Stop();
        }
예제 #7
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();
        }
예제 #8
0
 private void Tick(object sender, MicroTimerEventArgs timerEventArgs)
 {
     if (Events.ContainsKey(Mode) && Mode != PlayStyle.Null && !form.IsDisposed)
     {
         Events[Mode].Update();
         if (updateTimer.Interval != Events[Mode].span)
             updateTimer.Interval = Events[Mode].span;
         Bitmap image = Events[Mode].Draw(preview.GetImage());
         if (image != null)
             preview.SetImage(image);
         lock (log)
         {
             if (log.Count > 0)
                 preview.UpdateSendingData(serialPort.IsOpen, serialPort.IsOpen ? (float)(1000.0d / (log.Sum() / log.Count)) : 0);
             else
                 preview.UpdateSendingData(false, 0);
         }
         /*if (!Sending && serialPort.IsOpen)
         {
             Sending = true;
             Thread t = new Thread(new ThreadStart(Send));
             t.Name = "SendingThread";
             t.Start();
         }*/
         if (SendThread != null)
         {
             if (!Sending && preview.IsUpdated())
             {
                 if (!form.IsDisposed)
                 {
                     form.Invoke((MethodInvoker)delegate ()
                     {
                         SendingImage = (Bitmap)preview.GetImage().Clone();
                     });
                     Sending = true;
                 }
             }
         }
     }
 }
예제 #9
0
 void mt_MicroTimerElapsed(object sender, MicroTimerEventArgs timerEventArgs)
 {
     long tickDiff = DateTime.Now.Ticks - this.ticksAtStartOfFrame;
     float newX = ((float)tickDiff / (float)ticksPerFrame);
     if (newX > 1)
     {
         //Console.WriteLine("reset");
         this.ticksAtStartOfFrame = DateTime.Now.Ticks;
         p.x = (newX) - 1;
     }
     else p.x = newX;
     if (this.Mode == ModuleMode.Active) NewPosition(this, new NewPositionEventArgs(this.p));
 }
예제 #10
0
 private void TimedEvent(object sender, MicroTimerEventArgs timerEventArgs)
 {
     ++amountCalled;
 }
예제 #11
0
    private void hiResTick(object sender, MicroTimerEventArgs timerEventArgs)
    {
        // Emulate a cycle
        emulator.Cycle();

        // If the screen was updated, draw it
        if (emulator.DrawRequired()) {
            byte[] screen_data = emulator.GetDisplay();

            for (int y = 0; y < SCREEN_HEIGHT; ++y)
            {
                for (int x = 0; x < SCREEN_WIDTH; ++x)
                {
                    if (screen_data[(y * SCREEN_WIDTH) + x] == 0)
                        screen.SetPixel(x, y, Color.Black);
                    else
                        screen.SetPixel(x, y, Color.White);
                }
            }

            QueueDraw();
        }
    }