/// <summary> /// 接收到数据 /// </summary> /// <param name="Sender"></param> /// <param name="e"></param> private void x_DataReceive(HttpWebClient Sender, DownLoadEventArgs e) { tCountdown.Stop(); try { if (!b) { lock (_SyncLockObject) { if (!b) { //Console.Write(DateTime.Now.ToString() + " 已接收数据: "); b = true; } } } string f = e.DownloadState.FileName; if (e.DownloadState.AttachmentName != null) { f = Path.GetDirectoryName(f) + @"\" + e.DownloadState.AttachmentName; } _File = f; using (var sw = new FileStream(f, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) { sw.Position = e.DownloadState.Position; sw.Write(e.DownloadState.Data, 0, e.DownloadState.Data.Length); sw.Close(); } string s = DateTime.Now.ToLongTimeString(); lock (_SyncLockObject) { i += e.DownloadState.Data.Length; //int aa = int.Parse(((double) i*100/Sender.FileLength).ToString()); string aa = i + " / " + Sender.FileLength + " 字节数据 "; string bb = "(" + ((double)i * 100 / Sender.FileLength).ToString("0.000") + "%)"; //pBar.Dispaly(aa, bb); //Console.Write("(" + ((double)i * 100 / Sender.FileLength).ToString("0.000") + "%)");//12:12:12 //Console.Write(bs + "\b\b\b\b\b\b\b\b\b\b" + i + " / " + Sender.FileLength + " 字节数据 " + bb + DigiLenth(bb) + s); //bs = new string('\b', Digits(i) + 3 + Digits(Sender.FileLength) + s.Length); Console.Write(aa + bb + s + DigiLenth(aa + bb + s) + "\b\b\b\b"); if (i == Sender.FileLength) { eventX.Set(); } } } catch { } tCountdown.Start();//重新计时 }
/// <summary> /// 发生异常 /// </summary> /// <param name="Sender"></param> /// <param name="e"></param> private void x_ExceptionOccurrs(HttpWebClient Sender, ExceptionEventArgs e) { try { //Console.WriteLine(e.Exception.Message); //发生异常重新下载相当于断点续传,你可以自己自行选择处理方式 var webclient = new HttpWebClient(); //DownloadFileChunk(string Address, string FileName, int FromPosition, int Length) webclient.DownloadFileChunk(FilePath, _File, e.DownloadState.Position, e.DownloadState.Length); e.ExceptionAction = ExceptionActions.Ignore; } catch { } }
/// <summary> /// 线程结束 /// </summary> /// <param name="Sender"></param> /// <param name="e"></param> private void x_ThreadProcessEnd(HttpWebClient Sender, ThreadProcessEventArgs e) { try { //if (e.thread.ThreadState == System.Threading.ThreadState.Stopped) if (_k++ == _K - 1) { var name = FilePath.Split('/')[FilePath.Split('/').Length - 1];//获取文件名 //下载文件并保存 Console.WriteLine(name + "下载完成!"); //Console.WriteLine("\nend"); eventX.Set(); } } catch { } }