Precision input timer.
This class is used to create highly accurate simulated data inputs aligned to the local clock.
One static instance of this internal class is created per encountered frame rate.
Inheritance: IDisposable
Ejemplo n.º 1
0
        // Static Methods

        /// <summary>
        /// Attach to a <see cref="PrecisionInputTimer"/> for the specified <paramref name="framesPerSecond"/>.
        /// </summary>
        /// <param name="framesPerSecond">Desired frames per second for the input timer.</param>
        /// <param name="exceptionHandler">Optional delegate to handle exception reporting from the timer.</param>
        /// /// <returns>A <see cref="PrecisionInputTimer"/> that can be used for the specified <paramref name="framesPerSecond"/>.</returns>
        public static PrecisionInputTimer Attach(int framesPerSecond, Action <Exception> exceptionHandler = null)
        {
            PrecisionInputTimer timer;

            lock (s_inputTimers)
            {
                // Get static input timer for given frames per second creating it if needed
                if (!s_inputTimers.TryGetValue(framesPerSecond, out timer))
                {
                    try
                    {
                        // Create a new precision input timer
                        timer = new PrecisionInputTimer(framesPerSecond);
                        timer.ExceptionHandler = exceptionHandler;

                        // Add timer state for given rate to static collection
                        s_inputTimers.Add(framesPerSecond, timer);
                    }
                    catch (Exception ex)
                    {
                        // Process exception for logging
                        if ((object)exceptionHandler != null)
                        {
                            exceptionHandler(new InvalidOperationException("Failed to create precision input timer due to exception: " + ex.Message, ex));
                        }
                        else
                        {
                            throw;
                        }
                    }
                }

                // Increment reference count for input timer at given frame rate
                if ((object)timer != null)
                {
                    timer.AddReference();
                }
            }

            return(timer);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Detach from the <see cref="PrecisionInputTimer"/>.
        /// </summary>
        /// <param name="timer">Timer instance to detach from.</param>
        /// <remarks>
        /// Timer reference will be set to <c>null</c> after detach.
        /// </remarks>
        public static void Detach(ref PrecisionInputTimer timer)
        {
            if ((object)timer != null)
            {
                lock (s_inputTimers)
                {
                    // Verify static frame rate timer for given frames per second exists
                    if (s_inputTimers.ContainsKey(timer.FramesPerSecond))
                    {
                        // Decrement reference count
                        timer.RemoveReference();

                        // If timer is no longer being referenced we stop it and remove it from static collection
                        if (timer.ReferenceCount == 0)
                        {
                            timer.Dispose();
                            s_inputTimers.Remove(timer.FramesPerSecond);
                        }
                    }
                }
            }

            timer = null;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Detatch from the <see cref="PrecisionInputTimer"/>.
        /// </summary>
        /// <param name="timer">Timer instance to detach from.</param>
        /// <remarks>
        /// Timer reference will be set to <c>null</c> after detatch.
        /// </remarks>
        public static void Detach(ref PrecisionInputTimer timer)
        {
            if ((object)timer != null)
            {
                lock (s_inputTimers)
                {
                    // Verify static frame rate timer for given frames per second exists
                    if (s_inputTimers.ContainsKey(timer.FramesPerSecond))
                    {
                        // Decrement reference count
                        timer.RemoveReference();

                        // If timer is no longer being referenced we stop it and remove it from static collection
                        if (timer.ReferenceCount == 0)
                        {
                            timer.Dispose();
                            s_inputTimers.Remove(timer.FramesPerSecond);
                        }
                    }
                }
            }

            timer = null;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Attach to a <see cref="PrecisionInputTimer"/> for the specified <paramref name="framesPerSecond"/>.
        /// </summary>
        /// <param name="framesPerSecond">Desired frames per second for the input timer.</param>
        /// <param name="exceptionHandler">Optional delegate to handle exception reporting from the timer.</param>        
        /// /// <returns>A <see cref="PrecisionInputTimer"/> that can be used for the specified <paramref name="framesPerSecond"/>.</returns>
        public static PrecisionInputTimer Attach(int framesPerSecond, Action<Exception> exceptionHandler)
        {
            PrecisionInputTimer timer;

            lock (s_inputTimers)
            {
                // Get static input timer for given frames per second creating it if needed
                if (!s_inputTimers.TryGetValue(framesPerSecond, out timer))
                {
                    try
                    {
                        // Create a new precision input timer
                        timer = new PrecisionInputTimer(framesPerSecond);
                        timer.ExceptionHandler = exceptionHandler;

                        // Add timer state for given rate to static collection
                        s_inputTimers.Add(framesPerSecond, timer);
                    }
                    catch (Exception ex)
                    {
                        // Process exception for logging
                        if ((object)exceptionHandler != null)
                            exceptionHandler(new InvalidOperationException("Failed to create precision input timer due to exception: " + ex.Message, ex));
                        else
                            throw;
                    }
                }

                // Increment reference count for input timer at given frame rate
                timer.AddReference();
            }

            return timer;
        }