private void recordThread() { stopwatch.Start(); while (true) { if (MainWindow.recording) { if (videoWriterFlag) { reset(); } //Stopwatch stopWatch = new Stopwatch(); //stopWatch.Start(); //screenCaptureUtils.captureScreen(); //stopWatch.Stop(); //Debug.WriteLine("frameTime > " + stopWatch.Elapsed.TotalSeconds + " s"); videoFileWriter.writeToFrameBuffer(screenCaptureUtils.captureScreen()); timeDelta = stopwatch.ElapsedMilliseconds - oldTime; if (timeDelta < frameTime) { Thread.Sleep((int)(frameTime - timeDelta)); MainWindow.currentRecordingFramerate = 1000f / (timeDelta + (frameTime - timeDelta)); } else { MainWindow.currentRecordingFramerate = 1000f / timeDelta; } calculateAverageFramerate(); oldTime = stopwatch.ElapsedMilliseconds; } else { videoWriterFlag = true; if (videoFileWriter != null && videoFileWriter.isOpen) { videoFileWriter.lastFrame = true; while (!videoFileWriter.finished) { Thread.Sleep(30); } Debug.WriteLine("finalized"); videoFileWriter.finalize(averageFramerate); } Thread.Sleep(100); } } }