private void btnStart_Click(object sender, EventArgs e) { this.btnStart.Enabled = false; isCancel = false; threadCompleteNum = 0; cidCompleteNum = 0; //ciderrorlist可以不用重新赋值 listBoxCtrl.Items.Insert(0, DateTime.Now.ToString(Constants.DATE_TIME_FORMAT) + "正在计算需下载个数……"); Application.DoEvents(); List <string> totalcidlist = GetCheckedCids(treeListItemCat.Nodes); if (totalcidlist.Count == 0) { listBoxCtrl.Items.Insert(0, "没有需要下载的类目!"); XtraMessageBox.Show("没有需要下载的类目!", Constants.SYSTEM_PROMPT, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } listBoxCtrl.Items.Insert(0, string.Format("需要下载类目总数{0}个", totalcidlist.Count)); //进度条最大值 this.progressBarTotal.Properties.Maximum = totalcidlist.Count; //线程数 threadnum = (int)spinEditThreadCount.Value; int size = 0; if (totalcidlist.Count < threadnum) { threadnum = totalcidlist.Count; size = 1; } else { size = totalcidlist.Count % threadnum == 0 ? totalcidlist.Count / threadnum : totalcidlist.Count / threadnum + 1; } for (int i = 0; i < threadnum; i++) { List <string> cidlist = totalcidlist.Skip(i * size).Take(size).ToList(); BackgroundWorker batchworker = new BackgroundWorker(); batchworker.WorkerReportsProgress = true; batchworker.WorkerSupportsCancellation = true; batchworker.DoWork += new DoWorkEventHandler(batchworker_DoWork); batchworker.ProgressChanged += new ProgressChangedEventHandler(batchworker_ProgressChanged); batchworker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(batchworker_RunWorkerCompleted); listBoxCtrl.Items.Insert(0, string.Format("{0}线程{1}开始下载……", DateTime.Now.ToString(Constants.DATE_TIME_FORMAT), i + 1)); MyCidTask cidtask = new MyCidTask(); cidtask.cidlist = cidlist; cidtask.threadId = i + 1; batchworker.RunWorkerAsync(cidtask); } }
void batchworker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = (BackgroundWorker)sender; MyCidTask cidtask = (MyCidTask)e.Argument; List <string> threadcidlist = cidtask.cidlist; #region 多类目下载一次存储方式,不采用 ////分times次下载 //int times = 100; //int size = 0; //if (threadcidlist.Count >= times) //{ // size = threadcidlist.Count % times == 0 ? threadcidlist.Count / times : threadcidlist.Count / times + 1; //} //else //{ // size = 1; // times = threadcidlist.Count; //} #endregion int times = threadcidlist.Count; //int temp = 0;//作用是避免进度值propgress没有改变时得重复报告 for (int i = 0; i < times; i++) { cidCompleteNum++; if (isCancel) { e.Cancel = true; break; } #region 多类目下载一次存储方式,不采用 //List<string> cidlist = threadcidlist.Skip(i * size).Take(size).ToList(); //List<string> valueWhereInCids = ItemPropValueService.GetPropValueWhereInCids(cidlist); //List<string> valueCidList = cidlist.Except(valueWhereInCids).ToList(); //DownItemPropvalue(valueCidList, worker); //List<string> propWhereInCids = ItemPropService.GetPropWhereInCids(cidlist); //List<string> propCidList = cidlist.Except(propWhereInCids).ToList(); //DownItemProp(propCidList, worker); //UpdatePropValueIsParent(cidlist, worker); //ReturnType type = ItemCatService.UpdateItemCatPropTag(cidlist); #endregion string cid = threadcidlist[i]; try { UIHelper.DownPropsAndValues(cid, worker); } catch (Exception ex) { if (ciderrorlist == null) { ciderrorlist = new List <string>(); } ciderrorlist.Add(cid); worker.ReportProgress(int.Parse(cid), string.Format("异常信息{0}", ex.Message)); continue; } //更新ItemCat中此cid下的属性已完全下载 ItemCatService.UpdateItemCatPropTag(cid, true); /*进度报告*/ //int percentage = (int)((float)(i + 1) * 100 / (float)times); //if (percentage > temp) //{ // worker.ReportProgress(percentage, cidtask.threadId); //} //temp = percentage; worker.ReportProgress(cidCompleteNum, cidtask.threadId); } e.Result = cidtask.threadId; }