private void btnRetry_Click(object sender, EventArgs e) { this.btnRetry.Enabled = false; threadnum--; //线程数必须减1 itemCompleteNum = 0; //完成数置0 string inforMsg = string.Format("{0} {1}", DateTime.Now.ToString("HH:mm:ss"), "重新下载失败的宝贝!"); listBoxDetail.Items.Insert(0, inforMsg); /*再开线程处理*/ BackgroundWorker workerExtent = new BackgroundWorker(); workerExtent.WorkerReportsProgress = true; workerExtent.WorkerSupportsCancellation = true; workerExtent.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted); workerExtent.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged); workerExtent.DoWork += new DoWorkEventHandler(worker_DoWork); /*参数传递*/ ItemTask tradeTask = new ItemTask(); tradeTask.shopNic = failedDownload.shopNick; tradeTask.iidList = failedDownload.iidList; tradeTask.sessionKey = failedDownload.sessionKey; tradeTask.totalNum = failedDownload.iidList.Count; tradeTask.threadIndex = 1; //重新设定进度条最大值 this.progressBarTotal.Properties.Maximum = tradeTask.totalNum; //重新实例化 failedDownload = new FailDownloadeItem(); //线程开启 workerExtent.RunWorkerAsync(tradeTask); }
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { threadCompleteNum++; if (threadCompleteNum == threadnum) { this.btnDowmItems.Enabled = true; } if (e.Cancelled) { listBoxDetail.Items.Insert(0, "下载被取消!"); if (threadCompleteNum == threadnum) { XtraMessageBox.Show("下载被取消!", Constants.SYSTEM_PROMPT, MessageBoxButtons.OK, MessageBoxIcon.Information); } } else if (e.Error != null) { listBoxDetail.Items.Insert(0, e.Error.Message); if (threadCompleteNum == threadnum) { XtraMessageBox.Show("下载出错!", Constants.SYSTEM_PROMPT, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } else { listBoxDetail.Items.Insert(0, string.Format("{0}线程号{1}下载成功!", DateTime.Now.ToString(Constants.DATE_TIME_FORMAT), e.Result ?? string.Empty)); if (threadCompleteNum == threadnum) { XtraMessageBox.Show("下载成功!", Constants.SYSTEM_PROMPT, MessageBoxButtons.OK, MessageBoxIcon.Information); } } if (threadCompleteNum == threadnum && failedDownload.iidList != null && failedDownload.iidList.Count > 0) { this.btnRetry.Enabled = true; //提示是否要重新下载 string message = string.Format("您的店铺{0}共有{1}个宝贝没有下载成功,是否要重新下载?", failedDownload.shopNick, failedDownload.iidList.Count); if (XtraMessageBox.Show(message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK) { this.btnRetry.Enabled = false; threadnum--; //线程数必须减1 itemCompleteNum = 0; //完成数置0 string inforMsg = string.Format("{0} {1}", DateTime.Now.ToString("HH:mm:ss"), "重新下载失败的宝贝!"); listBoxDetail.Items.Insert(0, inforMsg); /*再开线程处理*/ BackgroundWorker workerExtent = new BackgroundWorker(); workerExtent.WorkerReportsProgress = true; workerExtent.WorkerSupportsCancellation = true; workerExtent.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted); workerExtent.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged); workerExtent.DoWork += new DoWorkEventHandler(worker_DoWork); /*参数传递*/ ItemTask tradeTask = new ItemTask(); tradeTask.shopNic = failedDownload.shopNick; tradeTask.iidList = failedDownload.iidList; tradeTask.sessionKey = failedDownload.sessionKey; tradeTask.totalNum = failedDownload.iidList.Count; tradeTask.threadIndex = 1; //重新设定进度条最大值 this.progressBarTotal.Properties.Maximum = tradeTask.totalNum; //重新实例化 failedDownload = new FailDownloadeItem(); //线程开启 workerExtent.RunWorkerAsync(tradeTask); } } }