/// <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 事件.如果状态是Completed,则引发 DownloadCompleted事件。 /// </summary> /// <param name="e"></param> protected virtual void OnDownloadProgressChanged( MultiThreadedWebDownloaderProgressChangedEventArgs e) { if (DownloadProgressChanged != null) { DownloadProgressChanged(this, e); } }
/// <summary> /// 处理DownloadProgressChanged 事件。 /// </summary> void DownloadProgressChanged(object sender, MultiThreadedWebDownloaderProgressChangedEventArgs e) { // 每隔一秒刷新一次主要信息 。 if (DateTime.Now > lastNotificationTime.AddSeconds(1)) { lbSummary.Text = String.Format("接收: {0}KB 总共: {1}KB 速度: {2}KB/s 线程个数: {3}", e.ReceivedSize / 1024, e.TotalSize / 1024, e.DownloadSpeed / 1024, downloader.DownloadThreadsCount); prgDownload.Value = (int)(e.ReceivedSize * 100 / e.TotalSize); lastNotificationTime = DateTime.Now; } }