/// <summary> /// 处理所有 HttpDownloadClients的 DownloadProgressChanged事件,并计算速度 /// </summary> void client_DownloadProgressChanged(object sender, HttpDownloadClientProgressChangedEventArgs e) { lock (locker) { DownloadedSize += e.Size; bufferCount++; if (bufferCount == BufferCountPerNotification) { if (DownloadProgressChanged != null) { int speed = 0; DateTime current = DateTime.Now; TimeSpan interval = current - lastNotificationTime; if (interval.TotalSeconds < 60) { speed = (int)Math.Floor((this.DownloadedSize - this.lastNotificationDownloadedSize) / interval.TotalSeconds); } lastNotificationTime = current; lastNotificationDownloadedSize = this.DownloadedSize; var downloadProgressChangedEventArgs = new MultiThreadedWebDownloaderProgressChangedEventArgs( DownloadedSize, TotalSize, speed); this.OnDownloadProgressChanged(downloadProgressChangedEventArgs); } //重新设置bufferCount. bufferCount = 0; } } }
/// <summary> /// 引发 DownloadProgressChanged 事件。 /// </summary> protected virtual void OnDownloadProgressChanged(HttpDownloadClientProgressChangedEventArgs e) { if (DownloadProgressChanged != null) { DownloadProgressChanged(this, e); } }