Пример #1
0
        private void OnRequestFrame(object sender, Communication.MessageHandler.MessageEventArgs EventArgs)
        {
            if (System.Threading.Monitor.TryEnter(connectionLock))
            {
                try
                {
                    if (com != null)
                    {
                        byte   count = EventArgs.data[0];
                        byte[] data  = null;
                        if (nextFrame == null)
                        {
                            data = BuildNextFrame(count);
                        }
                        else
                        {
                            data = nextFrame;
                        }
                        com.SendMessage((byte)Commands.FRM_RESP, data);

                        long now = frameTimer.ElapsedTicks;
                        if (frameTimings.Count > 0)
                        {
                            long   difference      = now - frameTimings.Peek();
                            long   ticksPerFrame   = difference / frameTimings.Count;
                            double secondsPerFrame = ((double)ticksPerFrame / Stopwatch.Frequency);
                            double fps             = 1 / secondsPerFrame;

                            difference      = now - lastTimestamp;
                            secondsPerFrame = ((double)difference / Stopwatch.Frequency);
                            double fpslast = 1 / secondsPerFrame;

                            lock (fpsLock)
                            {
                                framesPerSecond = fps;
                                if (fpslast < fps * 0.75)
                                {
                                    framesDroppedBelow75.Push(fpslast);
                                }
                            }
                        }
                        lastTimestamp = now;
                        frameTimings.Enqueue(now);
                        while (frameTimings.Count > frameTimingsMax)
                        {
                            frameTimings.Dequeue();
                        }


                        nextFrame = BuildNextFrame(count);
                    }
                }
                finally
                {
                    System.Threading.Monitor.Exit(connectionLock);
                }
            }
            this.labelFPS.InvokeIfRequired(UpdateFramesPerSecond);
        }