// 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); }
/// <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; }