private void ProcessUpdateTask(object sender, DoWorkEventArgs e) { var worker = sender as BackgroundWorker; var prevTime = Stopwatch.StartNew(); while (!e.Cancel) { if (worker.CancellationPending) { e.Cancel = true; break; } prevTime.Restart(); if (!IsRunning & !_lastUpdateBeforePausing) { goto FrameLimitStreamUpdate; } _lastUpdateBeforePausing = false; int timeToWait; if (!RefreshRam()) { goto FrameLimitStreamUpdate; } OnUpdate?.Invoke(this, new EventArgs()); FrameLimitStreamUpdate: // Calculate delay to match correct FPS prevTime.Stop(); timeToWait = (int)RefreshRateConfig.RefreshRateInterval - (int)prevTime.ElapsedMilliseconds; timeToWait = Math.Max(timeToWait, 0); // Calculate Fps lock (_fpsQueueLocker) { if (_fpsTimes.Count() >= 10) { _fpsTimes.Dequeue(); } _fpsTimes.Enqueue(prevTime.ElapsedMilliseconds + timeToWait); } FpsUpdated?.Invoke(this, new EventArgs()); if (timeToWait > 0) { Thread.Sleep(timeToWait); } else { Thread.Yield(); } } OnClose?.BeginInvoke(this, new EventArgs(), null, null); }
private void ProcessUpdate() { Stopwatch frameStopwatch = Stopwatch.StartNew(); while (!disposedValue) { try { int timeToWait; lock (_mStreamProcess) { frameStopwatch.Restart(); if ((!IsEnabled || !IsRunning) && !_lastUpdateBeforePausing) { goto FrameLimitStreamUpdate; } _lastUpdateBeforePausing = false; if (!RefreshRam()) { goto FrameLimitStreamUpdate; } OnUpdate?.Invoke(this, new EventArgs()); FrameLimitStreamUpdate: // Calculate delay to match correct FPS frameStopwatch.Stop(); timeToWait = (int)RefreshRateConfig.RefreshRateInterval - (int)frameStopwatch.ElapsedMilliseconds; timeToWait = Math.Max(timeToWait, 0); // Calculate Fps if (_fpsTimes.Count() >= 10) { double garbage; _fpsTimes.TryDequeue(out garbage); } _fpsTimes.Enqueue(frameStopwatch.ElapsedMilliseconds + timeToWait); FpsUpdated?.Invoke(this, new EventArgs()); } if (timeToWait > 0) { Thread.Sleep(timeToWait); } else { Thread.Yield(); } } catch (Exception) { Monitor.Exit(_mStreamProcess); Debugger.Break(); } } }
private void ProcessUpdate() { Stopwatch frameStopwatch = Stopwatch.StartNew(); while (!disposedValue) { frameStopwatch.Restart(); Application.DoEvents(); //try //{ double timeToWait; lock (_mStreamProcess) { if ((!IsEnabled || !IsRunning) && !_lastUpdateBeforePausing) { goto FrameLimitStreamUpdate; } _lastUpdateBeforePausing = false; if (!RefreshRam()) { goto FrameLimitStreamUpdate; } OnUpdate?.Invoke(this, new EventArgs()); FrameLimitStreamUpdate: // Calculate delay to match correct FPS frameStopwatch.Stop(); double timePassed = (frameStopwatch.ElapsedTicks / (double)Stopwatch.Frequency); timeToWait = RefreshRateConfig.RefreshRateInterval - timePassed; timeToWait = Math.Max(timeToWait, 0); // Calculate Fps if (_fpsTimes.Count() >= 10) { double garbage; _fpsTimes.TryDequeue(out garbage); } _fpsTimes.Enqueue(timePassed + timeToWait); FpsUpdated?.Invoke(this, new EventArgs()); } if (timeToWait > 0) { Thread.Sleep(new TimeSpan((long)(timeToWait * 10000000))); } else { Thread.Yield(); } //} //catch (Exception exception) //{ // Debugger.Break(); // MessageBox.Show($"An exception occured in {nameof(ProcessUpdate)}:\n{exception.ToString()}"); //} } }