public void OnIdle(object state) { UiThread.RunOnIdle(OnIdle); if (!haveDrawn) { return; } double NumSecondsPassedSinceLastUpdate = 0.0f; int ThisSystemTickCount = Environment.TickCount; // handle the counter rolling over if (ThisSystemTickCount < m_LastSystemTickCount) { m_LastSystemTickCount = ThisSystemTickCount; } // figure out how many seconds have passed NumSecondsPassedSinceLastUpdate = (double)((ThisSystemTickCount - m_LastSystemTickCount) / 1000.0f); // add to it what we had left over from last time. NumSecondsPassedSinceLastUpdate += m_SecondsLeftOverFromLastUpdate; // limit it to the max that we are willing to consider double MaxSecondsToCatchUpOn = m_MaxUpdatesPerDraw * m_SecondsPerUpdate; if (NumSecondsPassedSinceLastUpdate > MaxSecondsToCatchUpOn) { NumSecondsPassedSinceLastUpdate = MaxSecondsToCatchUpOn; m_SecondsLeftOverFromLastUpdate = 0.0f; } // Reset our last tick count. Do this as soon as we can, to make the time more accurate. m_LastSystemTickCount = ThisSystemTickCount; bool WasUpdate = false; // if enough time has gone by that we are willing to do an update while (NumSecondsPassedSinceLastUpdate >= m_SecondsPerUpdate && m_PotentialUpdatesBudgetGraph != null) { WasUpdate = true; m_PotentialUpdatesStopWatch.Restart(); // call update with time slices that are as big as m_SecondsPerUpdate OnUpdate(m_SecondsPerUpdate); m_PotentialUpdatesStopWatch.Stop(); double Seconds = (double)(m_PotentialUpdatesStopWatch.Elapsed.TotalMilliseconds / 1000); if (Seconds == 0) { Seconds = 1; } m_PotentialUpdatesBudgetGraph.AddData(m_PotentialUpdatesPerSecondString, 1.0f / Seconds); string Lable = string.Format("U:{0:F2}", m_PotentialUpdatesBudgetGraph.GetAverageValue(m_PotentialUpdatesPerSecondString)); m_ShowPotentialUpdatesBudgetGraph.Text = Lable; m_NumSecondsSinceStart += m_SecondsPerUpdate; // take out the amount of time we updated and check again NumSecondsPassedSinceLastUpdate -= m_SecondsPerUpdate; } // if there was an update do a draw if (WasUpdate) { m_PotentialDrawsStopWatch.Restart(); //OnDraw(NewGraphics2D()); Invalidate(); m_PotentialDrawsStopWatch.Stop(); double Seconds = (double)(m_PotentialDrawsStopWatch.Elapsed.TotalMilliseconds / 1000); if (Seconds == 0) { Seconds = 1; } m_PotentialDrawsBudgetGraph.AddData(m_PotentialDrawsPerSecondString, 1.0f / Seconds); string Lable = string.Format("D:{0:F2}", m_PotentialDrawsBudgetGraph.GetAverageValue(m_PotentialDrawsPerSecondString)); m_ShowPotentialDrawsBudgetGraph.Text = Lable; m_ActualDrawsStopWatch.Stop(); Seconds = (double)(m_ActualDrawsStopWatch.Elapsed.TotalMilliseconds / 1000); if (Seconds == 0) { Seconds = 1; } m_ActualDrawsBudgetGraph.AddData(m_ActualDrawsPerSecondString, 1.0f / Seconds); Lable = string.Format("A:{0:F2}", m_ActualDrawsBudgetGraph.GetAverageValue(m_ActualDrawsPerSecondString)); m_ShowActualDrawsBudgetGraph.Text = Lable; m_ActualDrawsStopWatch.Restart(); } else // if there is more than 3 ms before the next update could happen then sleep for 1 ms. { double ThreeMiliSeconds = 3 / 1000.0f; if (ThreeMiliSeconds < m_SecondsPerUpdate - NumSecondsPassedSinceLastUpdate) { System.Threading.Thread.Sleep(1); } } // remember the time that we didn't use up yet m_SecondsLeftOverFromLastUpdate = NumSecondsPassedSinceLastUpdate; }
public void OnIdle() { if (!haveDrawn) { return; } double numSecondsPassedSinceLastUpdate = 0; int ThisSystemTickCount = Environment.TickCount; // handle the counter rolling over if (ThisSystemTickCount < lastSystemTickCount) { lastSystemTickCount = ThisSystemTickCount; } // figure out how many seconds have passed numSecondsPassedSinceLastUpdate = (double)((ThisSystemTickCount - lastSystemTickCount) / 1000.0); // add to it what we had left over from last time. numSecondsPassedSinceLastUpdate += secondsLeftOverFromLastUpdate; // limit it to the max that we are willing to consider double MaxSecondsToCatchUpOn = maxUpdatesPerDraw * secondsPerUpdate; if (numSecondsPassedSinceLastUpdate > MaxSecondsToCatchUpOn) { numSecondsPassedSinceLastUpdate = MaxSecondsToCatchUpOn; secondsLeftOverFromLastUpdate = 0.0; } // Reset our last tick count. Do this as soon as we can, to make the time more accurate. lastSystemTickCount = ThisSystemTickCount; bool WasUpdate = false; // if enough time has gone by that we are willing to do an update while (numSecondsPassedSinceLastUpdate >= secondsPerUpdate && potentialUpdatesBudgetGraph != null) { WasUpdate = true; potentialUpdatesStopWatch.Restart(); // call update with time slices that are as big as secondsPerUpdate OnUpdate(secondsPerUpdate); potentialUpdatesStopWatch.Stop(); double Seconds = (double)(potentialUpdatesStopWatch.Elapsed.TotalMilliseconds / 1000); if (Seconds == 0) { Seconds = 1; } potentialUpdatesBudgetGraph.AddData(potentialUpdatesPerSecondString, 1.0 / Seconds); string Lable = string.Format("U:{0:F2}", potentialUpdatesBudgetGraph.GetAverageValue(potentialUpdatesPerSecondString)); showPotentialUpdatesBudgetGraph.Text = Lable; numSecondsSinceStart += secondsPerUpdate; // take out the amount of time we updated and check again numSecondsPassedSinceLastUpdate -= secondsPerUpdate; } // if there was an update do a draw if (WasUpdate) { potentialDrawsStopWatch.Restart(); //OnDraw(NewGraphics2D()); Invalidate(); potentialDrawsStopWatch.Stop(); double Seconds = (double)(potentialDrawsStopWatch.Elapsed.TotalMilliseconds / 1000); if (Seconds == 0) { Seconds = 1; } potentialDrawsBudgetGraph.AddData(potentialDrawsPerSecondString, 1.0 / Seconds); string Lable = string.Format("D:{0:F2}", potentialDrawsBudgetGraph.GetAverageValue(potentialDrawsPerSecondString)); showPotentialDrawsBudgetGraph.Text = Lable; actualDrawsStopWatch.Stop(); Seconds = (double)(actualDrawsStopWatch.Elapsed.TotalMilliseconds / 1000); if (Seconds == 0) { Seconds = 1; } actualDrawsBudgetGraph.AddData(actualDrawsPerSecondString, 1.0 / Seconds); Lable = string.Format("A:{0:F2}", actualDrawsBudgetGraph.GetAverageValue(actualDrawsPerSecondString)); showActualDrawsBudgetGraph.Text = Lable; actualDrawsStopWatch.Restart(); } // remember the time that we didn't use up yet secondsLeftOverFromLastUpdate = numSecondsPassedSinceLastUpdate; }