private Bandwidth CalculateBandwidth(Bandwidth bandwidth, long progress, long fileLength, Stopwatch sw) { try { var measure = new BandwidthMeasure { ElapsedMs = sw.ElapsedMilliseconds, ProgressBytes = progress, TotalBytes = fileLength }; //we suppose that every measure is created in 100ms interval bandwidth.Measures.Add(measure); bandwidth.Mean1Second = CalculateBandwidthForPeriod(bandwidth.Measures, 1000, bandwidth.Mean1Second); bandwidth.Mean5Seconds = CalculateBandwidthForPeriod(bandwidth.Measures, 5000, bandwidth.Mean5Seconds); bandwidth.Mean30Seconds = CalculateBandwidthForPeriod(bandwidth.Measures, 30000, bandwidth.Mean30Seconds); bandwidth.Mean1Minute = CalculateBandwidthForPeriod(bandwidth.Measures, 60000, bandwidth.Mean1Minute); bandwidth.Remaining = bandwidth.Mean1Minute > 0 ? (fileLength - progress) / bandwidth.Mean1Minute : (long?)null; } catch (Exception ex) { OnExceptionThrown(ex); } return(bandwidth); }
public void OnBandwidthMeasure(Bandwidth bandwidth) { try { BandwidthMeasured?.Invoke(this, new BandwidthEventArgs { Bandwidth = bandwidth }); } catch (Exception ex) { OnExceptionThrown(ex); } }
private void StartBandwidthThread(ProgressChangedEventArgs changedEventArgs) { new Thread(_ => { try { var bandwidth = new Bandwidth { FileUrl = changedEventArgs.FileUrl, Measures = new List <BandwidthMeasure>() }; var sw = Stopwatch.StartNew(); bool completed = false; while (!stopProgressThread && !completed) { ProgressChangedEventArgs bandwidthArgs; lock (changedEventArgs) { bandwidthArgs = changedEventArgs.ShallowCopy(); } completed = bandwidthArgs.Progress == bandwidthArgs.FileLength; CalculateBandwidth(bandwidth, changedEventArgs.Progress, changedEventArgs.FileLength, sw); OnBandwidthMeasure(bandwidth); Thread.Sleep(100); } sw.Stop(); } catch (Exception ex) { OnExceptionThrown(ex); } }) { IsBackground = true }.Start(); }