/// <summary> /// Create a new <see cref="FrameRateTimer"/> class. /// </summary> /// <param name="framesPerSecond">Desired frame rate for <see cref="PrecisionTimer"/>.</param> /// <param name="processingInterval">Desired processing interval, if applicable.</param> /// <remarks> /// When the <paramref name="processingInterval"/> is set to -1, the frame rate timer interval will be calculated as a distribution /// of whole milliseonds over the specified number of <paramref name="framesPerSecond"/>. Otherwise the specified /// <paramref name="processingInterval"/> will be used as the timer interval. /// </remarks> public FrameRateTimer(int framesPerSecond, int processingInterval) { if (processingInterval == 0) throw new InvalidOperationException("A frame rate timer should not be created when using a processing interval of zero, i.e., processing data as fast as possible."); m_framesPerSecond = framesPerSecond; m_processingInterval = processingInterval; // Create a new precision timer for this timer state m_timer = new PrecisionTimer(); m_timer.AutoReset = true; if (processingInterval > 0) { // Establish fixed timer period m_timer.Period = processingInterval; } else { // Attach handler for timer period assignments m_timer.Tick += SetTimerPeriod; // Calculate distributed wait time periods over specified number of frames per second m_framePeriods = new int[framesPerSecond]; for (int frameIndex = 0; frameIndex < framesPerSecond; frameIndex++) { m_framePeriods[frameIndex] = CalcWaitTimeForFrameIndex(frameIndex); } // Establish initial timer period m_lastFramePeriod = m_framePeriods[0]; m_timer.Period = m_lastFramePeriod; } // Start timer m_timer.Start(); }
/// <summary> /// Create a new <see cref="FrameRateTimer"/> class. /// </summary> /// <param name="framesPerSecond">Desired frame rate for <see cref="PrecisionTimer"/>.</param> public FrameRateTimer(int framesPerSecond) { // Create a new precision timer for this timer state m_timer = new PrecisionTimer(); m_timer.AutoReset = true; // Attach handler for timer period assignments m_timer.Tick += SetTimerPeriod; m_framesPerSecond = framesPerSecond; m_framePeriods = new int[framesPerSecond]; // Calculate new wait time periods for new number of frames per second for (int frameIndex = 0; frameIndex < framesPerSecond; frameIndex++) { m_framePeriods[frameIndex] = CalcWaitTimeForFrameIndex(frameIndex); } // Establish initial timer period m_lastFramePeriod = m_framePeriods[0]; m_timer.Period = m_lastFramePeriod; // Start timer m_timer.Start(); }