c# http下载2 创建多个下载任务
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Drawing; 5 using System.Data; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 using httpTool; 11 using System.Threading; 12 13 namespace HttpDownLoad 14 { 15 public partial class ListItem : UserControl 16 { 17 private string urlValue; 18 private string savePathValue; 19 private bool stateValue; //false=>未下载完成;true=>下载完成 20 21 HttpHelper downLoadManager; 22 23 //下载文件信息 24 public class downLoadFileInfo 25 { 26 public string id; 27 public string name; 28 public string path; 29 public string url; 30 public bool state; 31 } 32 33 public ListItem() 34 { 35 InitializeComponent(); 36 } 37 38 private void ListItem_Load(object sender, EventArgs e) 39 { 40 this.label5.Text = IdValue; 41 } 42 43 //下载地址 44 public string UrlValue 45 { 46 get 47 { 48 return urlValue; 49 } 50 51 set 52 { 53 urlValue = value; 54 } 55 } 56 57 //任务编号 58 public string IdValue 59 { 60 get 61 { 62 return label5.Text; 63 } 64 65 set 66 { 67 label5.Text = value; 68 } 69 } 70 71 //文件名称 72 public string NameValue 73 { 74 get 75 { 76 return GetFileName(savePathValue); 77 } 78 } 79 80 //文件保存位置 81 public string SavePathValue 82 { 83 get 84 { 85 return savePathValue; 86 } 87 88 set 89 { 90 savePathValue = value; 91 } 92 } 93 94 //下载状态 95 public bool StateValue 96 { 97 get 98 { 99 return stateValue; 100 } 101 102 set 103 { 104 stateValue = value; 105 } 106 } 107 108 //下载开始 109 public void DownLoadStart() 110 { 111 //下载状态 112 this.StateValue = false; 113 //下载初始化 114 downLoadManager = new HttpHelper(UrlValue, SavePathValue); 115 downLoadManager.timer = new System.Windows.Forms.Timer(); 116 downLoadManager.processShow += processShow; 117 downLoadManager.processCompleted += processComplete; 118 downLoadManager.processFailed += processFailed; 119 downLoadManager.init(); 120 Thread.Sleep(1000); 121 downLoadManager.DownLoadStart(); 122 123 } 124 125 //更新暂停、继续 126 private void button1_Click(object sender, EventArgs e) 127 { 128 if (this.StateValue == false) 129 { //下载状态 130 this.button1.Text = "继续"; 131 downLoadManager.DownLoadPause(); 132 } 133 else 134 { //停止状态 135 this.button1.Text = "暂停"; 136 downLoadManager.DownLoadContinue(); 137 } 138 this.StateValue = !this.StateValue; 139 } 140 141 //下载信息显示 142 private void processShow(string totalNum, string num, int proc, string speed, string remainTime, string msg) 143 { 144 this.label2.Text = speed; 145 this.label7.Text = remainTime; 146 this.progressBar1.Value = proc; 147 } 148 149 //下载完成回调 150 private void processComplete() 151 { 152 this.StateValue = true; 153 MessageBox.Show(string.Format("文件:{0} 下载完成", NameValue)); 154 } 155 156 //下载失败回调 157 private void processFailed(string msg) 158 { 159 this.StateValue = true; 160 MessageBox.Show(string.Format("文件:{0} 下载失败", NameValue)); 161 } 162 163 //根据文件地址获取名称 164 private string GetFileName(string path) 165 { 166 167 return System.IO.Path.GetFileName(path); 168 } 169 170 //文件信息 171 private void pictureBox1_Click(object sender, EventArgs e) 172 { 173 downLoadFileInfo fileInfo = new downLoadFileInfo(); 174 175 fileInfo.id = IdValue; 176 fileInfo.name = NameValue; 177 fileInfo.path = savePathValue; 178 fileInfo.url = UrlValue; 179 fileInfo.state = StateValue; 180 181 InfoDialog info = new InfoDialog(fileInfo); 182 info.ShowDialog(); 183 } 184 185 //显示文件名称 186 private void pictureBox1_MouseHover(object sender, EventArgs e) 187 { 188 this.toolTip1.SetToolTip(this.pictureBox1, NameValue); 189 } 190 191 192 } 193 }
百度网盘地址:
https://pan.baidu.com/s/1xp2YZICZUHT8iaZgLYbE2A