Example #1
0
        /// <summary>
        /// Create a new <see cref="PrecisionInputTimer"/> class.
        /// </summary>
        /// <param name="framesPerSecond">Desired frame rate for <see cref="PrecisionTimer"/>.</param>
        internal PrecisionInputTimer(int framesPerSecond)
        {
            // Create synchronization objects
            m_timerTickLock    = new object();
            m_frameWaitHandleA = new ManualResetEventSlim(false);
            m_frameWaitHandleB = new ManualResetEventSlim(false);
            m_useWaitHandleA   = true;
            m_framesPerSecond  = framesPerSecond;

            // Create a new precision timer for this timer state
            m_timer            = new PrecisionTimer();
            m_timer.Resolution = 1;
            m_timer.Period     = 1;
            m_timer.AutoReset  = true;

            // Attach handler for timer ticks
            m_timer.Tick += m_timer_Tick;

            m_frameWindowSize   = (int)Math.Round(1000.0D / framesPerSecond) * 2;
            m_frameMilliseconds = Ticks.MillisecondDistribution(framesPerSecond);

            // Start high resolution timer on a separate thread so the start
            // time can synchronized to the top of the millisecond
            ThreadPool.QueueUserWorkItem(SynchronizeInputTimer);
        }
Example #2
0
        /// <summary>
        /// Releases the unmanaged resources used by the <see cref="PrecisionInputTimer"/> object and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        private void Dispose(bool disposing)
        {
            if (!m_disposed)
            {
                try
                {
                    if (disposing)
                    {
                        if ((object)m_timer != null)
                        {
                            m_timer.Tick -= m_timer_Tick;
                            m_timer.Dispose();
                        }
                        m_timer = null;

                        if ((object)m_frameWaitHandleA != null)
                        {
                            m_frameWaitHandleA.Set();
                            m_frameWaitHandleA.Dispose();
                        }
                        m_frameWaitHandleA = null;

                        if ((object)m_frameWaitHandleB != null)
                        {
                            m_frameWaitHandleB.Set();
                            m_frameWaitHandleB.Dispose();
                        }
                        m_frameWaitHandleB = null;
                    }
                }
                finally
                {
                    m_disposed = true;  // Prevent duplicate dispose.
                }
            }
        }