protected virtual void Dispose(bool disposing) { if (!disposing) return; if (_timer == null) return; _timer.Dispose(); _timer = null; }
public IDisposable SetupDownloadTransferProgress(ITransferProgress transferProgress, TimeSpan timeout) { var lastTime = Tools.Generic.GetCurrentUtcDateTime; long lastBytes = 0; transferProgress.Update(null, 0); transferProgress.FileSizeTransfered = 0; DownloadProgressChanged += (sender, args) => { var bytes = args.BytesReceived; var now = Tools.Generic.GetCurrentUtcDateTime; transferProgress.FileSizeTransfered = bytes; long? speed = null; if (lastBytes != 0) { var timeSpan = now - lastTime; var bytesChange = bytes - lastBytes; if (timeSpan.TotalMilliseconds > 0) speed = (long) (bytesChange/(timeSpan.TotalMilliseconds/1000.0)); } transferProgress.Update(speed, args.ProgressPercentage); lastBytes = bytes; lastTime = now; }; DownloadFileCompleted += (sender, args) => { transferProgress.Completed = true; }; var timer = new TimerWithElapsedCancellation(500, () => { if (!transferProgress.Completed && Tools.Generic.LongerAgoThan(lastTime, timeout)) { CancelAsync(); return false; } return !transferProgress.Completed; }); return timer; }
public RepoWatcher(StatusRepo repo) { _timer = new TimerWithElapsedCancellation(TimerTime, () => { repo.UpdateTotals(); return true; }); }