protected virtual void Dispose(bool disposing) {
            if (!disposing)
                return;
            if (_timer == null)
                return;

            _timer.Dispose();
            _timer = null;
        }
        static Timer SetupTransferProgress(IWebClient webClient, ITransferProgress transferProgress) {
            var lastTime = Tools.Generic.GetCurrentUtcDateTime;
            long lastBytes = 0;

            transferProgress.Progress = 0;
            transferProgress.FileSizeTransfered = 0;

            webClient.DownloadProgressChanged +=
                (sender, args) => {
                    var bytes = args.BytesReceived;
                    var now = Tools.Generic.GetCurrentUtcDateTime;

                    transferProgress.Progress = args.ProgressPercentage;
                    transferProgress.FileSizeTransfered = bytes;

                    if (lastBytes != 0) {
                        var timeSpan = now - lastTime;
                        var bytesChange = bytes - lastBytes;

                        if (timeSpan.TotalMilliseconds > 0)
                            transferProgress.Speed = (long) (bytesChange/(timeSpan.TotalMilliseconds/1000.0));
                    }

                    lastBytes = bytes;
                    lastTime = now;
                };

            webClient.DownloadFileCompleted += (sender, args) => { transferProgress.Completed = true; };

            var timer = new TimerWithElapsedCancellation(500, () => {
                if (!transferProgress.Completed
                    && Tools.Generic.LongerAgoThan(lastTime, timeout)) {
                    webClient.CancelAsync();
                    return false;
                }
                return !transferProgress.Completed;
            });
            return timer;
        }
Exemple #3
0
 public Snow(Canvas canvas, IEventAggregator eventBus) {
     _timer = new TimerWithElapsedCancellation(TimeSpan.FromMilliseconds(Interval), OnTimerTicker, null, false);
     _canvas = canvas;
     eventBus.Subscribe(this);
 }
 public RepoWatcher(StatusRepo repo) {
     _timer = new TimerWithElapsedCancellation(TimerTime, () => {
         repo.UpdateTotals();
         return true;
     });
 }
            protected virtual void Dispose(bool disposing) {
                if (!disposing)
                    return;

                if (_disposed)
                    return;
                _timer.Dispose();
                _timer = null;

                _disposed = true;
            }