public void Run(CancellationToken token) { if (IsRunning) { throw new Exception(nameof(DesktopDuplicatorReader) + " is already running!"); } IsRunning = true; _log.Debug("Started Desktop Duplication Reader."); Bitmap image = null; try { BitmapData bitmapData = new BitmapData(); while (!token.IsCancellationRequested) { var frameTime = Stopwatch.StartNew(); var newImage = GetNextFrame(image); if (newImage == null) { //there was a timeout before there was the next frame, simply retry! continue; } image = newImage; _logic.ProcessNewImage(newImage); int minFrameTimeInMs = 1; //1000/FPS var elapsedMs = (int)frameTime.ElapsedMilliseconds; if (elapsedMs < minFrameTimeInMs) { Thread.Sleep(minFrameTimeInMs - elapsedMs); } } } finally { image?.Dispose(); _desktopDuplicator?.Dispose(); _desktopDuplicator = null; _log.Debug("Stopped Desktop Duplication Reader."); IsRunning = false; } }
private Bitmap GetNextFrame(Bitmap reusableBitmap) { if (_desktopDuplicator == null) { _desktopDuplicator = new DesktopDuplicator(0, settings.Monitor); } try { return(_desktopDuplicator.GetLatestFrame(reusableBitmap)); } catch (Exception ex) { if (ex.Message != "_outputDuplication is null") { _log.Error(ex, "GetNextFrame() failed."); } _desktopDuplicator?.Dispose(); _desktopDuplicator = null; return(null); } }