private void Application_Idle(object sender, EventArgs e) { while (IsApplicationIdle()) { // Measure the amount of time since the last frame - we don't want to loop too quickly _TimeUtil.AccumulatedElapsedTimeMilli += _TimeUtil.GetDelta(); // Check if enough time has passed to do another update if (_TimeUtil.AccumulatedElapsedTimeMilli < _TimeUtil.TargetElapsedTimeMilli) { // ---> To sleep or simply exit? // int sleepTime = (int)(_TimeUtil.TargetElapsedTimeMilli - _TimeUtil.AccumulatedElapsedTimeMilli); // System.Threading.Thread.Sleep(sleepTime); break; } // ************** // Handle input OnInput(_InputUtil); _TimeUtil.ElapsedGameTimeMilli = _TimeUtil.TargetElapsedTimeMilli; _InputTimer += (float)_TimeUtil.ElapsedGameTimeMilli; // ************** // Handle updates _FpsUtil.Update(_TimeUtil); OnUpdate(_TimeUtil); // ************** // Handle Render OnRender(_Bitmap); // Invalidate the picture box and force a re-render pictureBox1.Invalidate(); // Show the FPS and update steps in the window title Text = $"FPS {_FpsUtil.FramesPerSecond} Emulation Mode: {_EmulationMode} Num CPU Cycles: {_NESCore.SystemClockCounter()}"; } }