C# ThreadPool 分批處理資料,所有資料執行完再返回

劉居正發表於2021-09-04

這是一個呼叫翻譯資料的功能,所有資料一次性提交會造成後臺服務壓力大,介面反應時間也長。

所以做了一個分批處理,等待所有批次的資料呼叫介面都返回後再執行下一步。

 

 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

 

相關文章