void mainWorker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = (BackgroundWorker)sender; threadCount = Convert.ToInt32(spinEditThreadCount.Text); string nick = string.Empty; string title = string.Empty; string sid = string.Empty; bool isSelected; TreeListNode node = null; //循环shop树,获得选中的店铺 IEnumerator treeListEnumerator = this.treeListShop.Nodes.GetEnumerator(); while (treeListEnumerator.MoveNext()) { if (mainWorker.CancellationPending) { e.Cancel = true; return; } try { node = treeListEnumerator.Current as TreeListNode; isSelected = Convert.ToBoolean(node[treeListShop.Columns["IsSelected"].AbsoluteIndex]); if (node[treeListShop.Columns["nick"].AbsoluteIndex] != null) { nick = node[treeListShop.Columns["nick"].AbsoluteIndex].ToString(); } if (node[treeListShop.Columns["title"].AbsoluteIndex] != null) { title = node[treeListShop.Columns["title"].AbsoluteIndex].ToString(); } if (node[treeListShop.Columns["sid"].AbsoluteIndex] != null) { sid = node[treeListShop.Columns["sid"].AbsoluteIndex].ToString(); } if (isSelected) { //线程总数量设为0 totalThreadCount = 0; //清空itemList itemList.Clear(); //清空子线程列表 workerList.Clear(); worker.ReportProgress(1, DateTime.Now.ToString(Constants.DATE_TIME_FORMAT) + " 正在获取店铺" + title + "的宝贝信息......"); //获取选中店铺所有的item itemRsp = TopService.ItemsAllGet(SystemHelper.GetSessionKey(nick), fields, 1, pageSize); if (itemRsp == null) { worker.ReportProgress(1, title + "店铺宝贝获取失败。"); } else { /*把item加入到itemlist中*/ if (itemRsp.Items != null && itemRsp.Items.Item != null) { foreach (Taobao.Entity.Item item in itemRsp.Items.Item) { itemList.Add(item); } } worker.ReportProgress(1, string.Format("检测到店铺内宝贝{0}个。", itemRsp.TotalResults)); //计算页数 pageNo = itemRsp.TotalResults % pageSize == 0 ? (itemRsp.TotalResults / pageSize) : ((itemRsp.TotalResults / pageSize) + 1); //如果超过一页,即超过200条 if (pageNo > 1) { //获取所有的item列表 worker.ReportProgress(1, "正在获取店铺宝贝列表......"); //下载其它页 for (int i = 2; i <= pageNo; i++) { itemRsp = TopService.ItemsAllGet(SystemHelper.GetSessionKey(nick), fields, i, pageSize); if (itemRsp.Items != null && itemRsp.Items.Item != null) { foreach (Taobao.Entity.Item item in itemRsp.Items.Item) { itemList.Add(item); } } } worker.ReportProgress(1, "店铺宝贝列表获取成功。"); } worker.ReportProgress(1, "系统正在启动多线程下载宝贝详细信息......"); //如果商品的数量小于 if (itemRsp.TotalResults < threadCount) { threadCount = itemRsp.TotalResults; } //计算每个子线程应该分配多少item去下载,itemPageSize itemPageSize = itemRsp.TotalResults % threadCount == 0 ? (itemRsp.TotalResults / threadCount) : ((itemRsp.TotalResults / threadCount) + 1); #region 开启下载子线程 //开启Item下载子线程 for (int i = 0; i < threadCount; i++) { //分页item,分配给每一个子线程。 List <Taobao.Entity.Item> items = itemList.Skip(i * itemPageSize).Take(itemPageSize).ToList(); //启动线程 BackgroundWorker itemWorker = new BackgroundWorker(); itemWorker.WorkerReportsProgress = true; itemWorker.WorkerSupportsCancellation = true; itemWorker.DoWork += new DoWorkEventHandler(worker_DoWork); //任务进行时,报告进度 itemWorker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged); //任务完称时要做的,比如提示等等 itemWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted); workerList.Add(itemWorker); //Item任务对象 ItemTask itemTask = new ItemTask(); itemTask.ItemList = items; itemTask.ShopSid = sid; itemTask.Index = i + 1; itemTask.totalCount = itemRsp.TotalResults; //每一个子线程传入相关的下载列表信息。 itemWorker.RunWorkerAsync(itemTask); //开启图片下载子线程 BackgroundWorker picWorker = new BackgroundWorker(); picWorker.WorkerReportsProgress = true; picWorker.WorkerSupportsCancellation = true; picWorker.DoWork += new DoWorkEventHandler(picWorker_DoWork); picWorker.ProgressChanged += new ProgressChangedEventHandler(picWorker_ProgressChanged); picWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(picWorker_RunWorkerCompleted); workerList.Add(picWorker); //ItemPic任务对象 ItemTask itemPicTask = new ItemTask(); itemPicTask.ItemList = items; itemPicTask.ShopSid = sid; picWorker.RunWorkerAsync(itemPicTask); } #endregion } progressCurrent = 0; //信号量,阻塞当前线程,这里阻塞的是下载主线程 autoReset.WaitOne(); } } catch (Exception ex) { BeginInvoke(new Action(() => { listBoxCtrl.Items.Insert(0, ex.Message); })); return; } } }
private void btnDowmItems_Click(object sender, EventArgs e) { try { if (cmbNick.Text == string.Empty) { XtraMessageBox.Show("请先选择店铺!", "提示"); return; } this.btnDowmItems.Enabled = false; //下载按钮不可用 this.btnRetry.Enabled = false; itemCompleteNum = 0; //下载完成数置0 threadCompleteNum = 0; //完成线程数置0 int pageSize = 200; int pageNo = 1; string fields = "iid,cid"; ItemRsp myrsp = null; string session = SystemHelper.GetSessionKey(cmbNick.Text); myrsp = TopService.ItemsAllGet(session, fields, 1, pageSize); if (myrsp.TotalResults == 0) { this.btnDowmItems.Enabled = true; XtraMessageBox.Show("无宝贝数据!"); return; } listBoxDetail.Items.Insert(0, string.Format("检测到店铺内宝贝数{0}个", myrsp.TotalResults)); listBoxDetail.Items.Insert(0, "开始下载……"); List <string> totalIidList = new List <string>(); /*把iid加入到iidlist中*/ if (myrsp.Items != null && myrsp.Items.Item != null) { foreach (Taobao.Entity.Item item in myrsp.Items.Item) { totalIidList.Add(item.Iid); } } #region 计算是否需要分页获取 if (myrsp.TotalResults % pageSize == 0) { pageNo = myrsp.TotalResults / pageSize; } else { pageNo = myrsp.TotalResults / pageSize + 1; } if (pageNo >= 2) { for (int i = 2; i <= pageNo; i++) { myrsp = TopService.ItemsAllGet(session, "iid", i, pageSize); if (myrsp.Items != null && myrsp.Items.Item != null) { foreach (Taobao.Entity.Item item in myrsp.Items.Item) { if (!totalIidList.Contains(item.Iid)) { totalIidList.Add(item.Iid); } } } } //for } //if #endregion //进度条最大值赋值 this.progressBarTotal.Properties.Maximum = totalIidList.Count; //线程数,全局变量 threadnum = (int)spinEditThreadCount.Value; int size = 0; if (totalIidList.Count < threadnum) { threadnum = totalIidList.Count; size = 1; } else { size = totalIidList.Count % threadnum == 0 ? totalIidList.Count / threadnum : totalIidList.Count / threadnum + 1; } for (int i = 0; i < threadnum; i++) { List <string> iidlist = totalIidList.Skip(i * size).Take(size).ToList(); BackgroundWorker worker = new BackgroundWorker(); worker.WorkerReportsProgress = true; worker.WorkerSupportsCancellation = true; worker.DoWork += new DoWorkEventHandler(worker_DoWork); worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged); worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted); listBoxDetail.Items.Insert(0, string.Format("{0}线程{1}开始下载……", DateTime.Now.ToString(Constants.DATE_TIME_FORMAT), i + 1)); ItemTask itemTask = new ItemTask(); itemTask.iidList = iidlist; itemTask.threadIndex = i + 1; itemTask.sessionKey = session; itemTask.shopNic = cmbNick.Text; worker.RunWorkerAsync(itemTask); } } catch (System.Exception ex) { this.btnDowmItems.Enabled = true; listBoxDetail.Items.Insert(0, ex.Message); XtraMessageBox.Show(ex.Message, Constants.SYSTEM_PROMPT, MessageBoxButtons.OK, MessageBoxIcon.Information); } }