private void btnCancelDownload_Click(object sender, EventArgs e) { if (IndexDownloadWorker.IsBusy) { IndexDownloadWorker.CancelAsync(); } if (AssetDowloadWorker.IsBusy) { AssetDowloadWorker.CancelAsync(); } }
private void btnIndexDownload_Click(object sender, EventArgs e) { if (IndexDownloadWorker.IsBusy || AssetDowloadWorker.IsBusy) { MessageBox.Show("正在下载文件,不能进行其他操作。", "", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } btnAssetDownLoad.Visible = false; btnCancelDownload.Visible = true; labelDownLoadInfo.Text = "正在下载索引文件,这需要数秒至数分钟的时间...期间请勿退出程序"; IndexDownloadWorker.RunWorkerAsync(); }
private void IndexDownloadWorker_DoWork(object sender, DoWorkEventArgs e) { ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Define.GetIndexUrl()); HttpWebResponse response = request.GetResponse() as HttpWebResponse; Stream responseStream = response.GetResponseStream(); Stream fileStream = new FileStream(Define.GetFullIndexPath(), FileMode.Create); long countSize = 0; long originSize = response.ContentLength / 100; byte[] bArr = new byte[10000]; int size = responseStream.Read(bArr, 0, bArr.Length); while (size > 0) { if (IndexDownloadWorker.CancellationPending) { e.Cancel = true; break; } fileStream.Write(bArr, 0, size); size = responseStream.Read(bArr, 0, bArr.Length); countSize = countSize + size; long result = countSize / originSize; IndexDownloadWorker.ReportProgress((int)result); } fileStream.Close(); responseStream.Close(); IndexDownloadWorker.ReportProgress(100); if (e.Cancel) { IndexDownloadWorker.ReportProgress(0); File.Delete(Define.GetFullIndexPath()); } }