private void Uploader_OnUpdateSpeed(object sender, OnUpdateSpeedEventArgs e) { Application.Current.Dispatcher.Invoke(() => { LabelSpeed.Content = e.Speed / 1024 / 1024f + " MB/s"; LabelEstimated.Content = $"{e.EstimatedTime} seconds"; }); }
private void RunTimers() { TimerCallback timerCallbackUpdater = new TimerCallback((target) => { float percent = (float)CurrentTotalBytes / Length; OnUpdateProgress.Invoke(this, new OnUpdateProgressEventArgs { Percent = percent, Bytes = CurrentTotalBytes }); if (Done || IsPaused || IsCancelled) { TimerUpdater.Dispose(); } }); TimerUpdater = new Timer(timerCallbackUpdater, null, 0, UpdateInterval); OnUpdateSpeedEventArgs onUpdateSpeedEventArgs = new OnUpdateSpeedEventArgs { PreviousBytes = CurrentTotalBytes, NowBytes = CurrentTotalBytes }; TimerCallback timerCallbackSpeedCalculator = new TimerCallback((target) => { onUpdateSpeedEventArgs.NowBytes = CurrentTotalBytes; Speed = onUpdateSpeedEventArgs.NowBytes - onUpdateSpeedEventArgs.PreviousBytes; if (Speed != 0) { EstimatedTime = (Length - CurrentTotalBytes) / Speed; } else { EstimatedTime = long.MaxValue; } onUpdateSpeedEventArgs.PreviousBytes = onUpdateSpeedEventArgs.NowBytes; if (Done || IsPaused || IsCancelled) { Speed = 0; EstimatedTime = long.MaxValue; TimerSpeedCalculator.Dispose(); } onUpdateSpeedEventArgs.Speed = Speed; onUpdateSpeedEventArgs.EstimatedTime = EstimatedTime; OnUpdateSpeed.Invoke(this, onUpdateSpeedEventArgs); }); TimerSpeedCalculator = new Timer(timerCallbackSpeedCalculator, null, 0, 1000); }