private void RunGame(NES nes, CancellationToken cancellationToken) { bool abortEmulation = false; var stopwatch = new Stopwatch(); var frameStopWatch = new Stopwatch(); while (!abortEmulation) { stopwatch.Restart(); for (int i = 0; i < FramesPerSecond; i++) { if (cancellationToken.IsCancellationRequested) { abortEmulation = true; break; } frameStopWatch.Restart(); _frameBuffer = nes.Frame(); while (frameStopWatch.ElapsedMilliseconds < _frameRate) { Thread.Sleep(0); } _gameScreen.Invalidate(); } stopwatch.Stop(); Console.WriteLine($"{stopwatch.ElapsedMilliseconds} ms elapsed to process 60 frames per second."); } }