Ejemplo n.º 1
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.º 2
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
                timer.AddReference();
            }

            return timer;
        }