编程语言
首页 > 编程语言> > C# ThreadPool 分批处理数据,所有数据执行完再返回

C# ThreadPool 分批处理数据,所有数据执行完再返回

作者:互联网

这是一个调用翻译数据的功能,所有数据一次性提交会造成后台服务压力大,接口反应时间也长。

所以做了一个分批处理,等待所有批次的数据调用接口都返回后再执行下一步。

 

 1         /// <summary>
 2         /// 自动翻译
 3         /// </summary>
 4         /// <param name="sender"></param>
 5         /// <param name="e"></param>
 6         private void Item_Click(object sender, EventArgs e)
 7         {14             List<hsCodeDictionaryEntity> hsCodeDictionary = GetMatchSelectedHawbItemsList();  //获取数据20             if (hsCodeDictionary.Count > 30)  //大于30条数据就分批处理
21             {
22                 ShowProcessBar(true, "正在自动匹配,请稍候...");
23                 int pageSize = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(Convert.ToDouble(hsCodeDictionary.Count) / 6)));
24                 ToPagingProcess(hsCodeDictionary, pageSize);   //主要是这个方法
25                 ShowProcessBar(true, "正在刷新界面数据,请稍候...");
26                 this.tbtnRefresh_Click(null, null); //刷新界面28             }
29             else
30             {
31                 #region
32                 TODO69                 #endregion
70             }
71         }

 

 

下面的代码就是处理分批执行,同时调用多次接口方法,所有返回结果之后就退出。

        #region

        private void ThreadMethod(object obj)
        {
            Param pra = (Param)obj;
            //等待5秒,用于模拟系统在处理事情
            try
            {
                OnMatchHsCodeByHawbHandler caller = new OnMatchHsCodeByHawbHandler(OnAssignMatchHsCode);   //调用后台接口
                IAsyncResult result = caller.BeginInvoke(pra.hsCodeDictionaryList, Constant.CurrentFlow, null, null);
                while (result.IsCompleted == false)
                {
                    Application.DoEvents();
                    Thread.Sleep(10);
                }
                Response response = caller.EndInvoke(result);

if (response != null) { switch (response.status) { case 1: //this.tbtnRefresh_Click(null, null); break; case 0: string errMsg = GetErrorInfor(response.errCode); logger.Error("自动匹配hscode保存出错。错误信息:" + errMsg + ",错误原因:" + response.error); break; } } else { logger.Error("自动匹配hscode保存出错。错误信息:" + Properties.Resources.WSReturnNullResponse + ",错误原因:" + response.error); } } catch (Exception ex) { logger.ErrorException("matchHsCodeByHawb_Click()", ex); } pra.mrEvent.Set(); lock (locker) { finishcount++; Monitor.Pulse(locker); //完成,通知等待队列,告知已完,执行下一个。 } } int _ThreadCount = 6; int finishcount = 0; object locker = new object(); List<ManualResetEvent> manualEvents = new List<ManualResetEvent>(); protected void ToPagingProcess<TEntity>(IEnumerable<TEntity> item, int pageSize) { finishcount = 0; if (item != null && item.Count() > 0) { var count = item.Count(); var pages = item.Count() / pageSize; if (count % pageSize > 0) { pages += 1; } for (int i = 1; i <= pages; i++) { var currentPageItem = item.Skip((i - 1) * pageSize).Take(pageSize); ManualResetEvent mre = new ManualResetEvent(false); manualEvents.Add(mre); Param pra = new Param(); pra.mrEvent = mre; pra.hsCodeDictionaryList = new List<hsCodeDictionaryEntity>(); foreach (var itm in currentPageItem) { pra.hsCodeDictionaryList.Add(itm as hsCodeDictionaryEntity); } ThreadPool.QueueUserWorkItem(ThreadMethod, pra); } lock (locker) { while (finishcount != _ThreadCount) { Monitor.Wait(locker);//等待 } } } } #endregion

 

标签:Count,pra,pageSize,C#,完再,ThreadPool,item,null,response
来源: https://www.cnblogs.com/qigao/p/15226213.html