本文介绍了C#Parallel foreach - System.AggregateException:发生一个或多个错误。 --->的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个混淆了很长时间的错误,仍然无法修复它。任何人都可以帮忙吗?

I got one bug confused me long time,still not fix it.Anyone can help ?

private string ppcManageShortSales(string strData)
       {
List<salesresultppc> listSalesResultPPC = new List<salesresultppc>();
                            string[] arrSalesInfo = salesInfo.Split('`');
                            var sales = new salesInfoPPC[arrSalesInfo.Length];
                            string[] arrRawData = new string[arrSalesInfo.Length];//luo add Aug 28,2013
                            int taskIndex = 0;
                            for (int tt = 0; tt < arrSalesInfo.Length; tt++)
                            {
                                salesInfoPPC sippc = new salesInfoPPC();
                                sippc.strDataResource = strDataResouce;
                                sippc.payout = payout;
                                sippc.dtSalesDetail = createSalesDetailTable();
                                sippc.drawdate = arrSelectedDrawDate[0];
                                sippc.listHotDigit = listHotDigit;
                                sippc.salesInfo = arrSalesInfo[tt];
                                sippc.tempPlayMethodArrangeId = tempPlayMethodArrangeId;
                                sippc.salesIndex = tt;//luo add Aug 28,2013
                                sales[taskIndex] = sippc;
                                taskIndex++;
                            }
Parallel.ForEach(sales, (salesInfoPPC) => listSalesResultPPC.Add(ppcManageShortSalesHelper(salesInfoPPC)));

                   foreach (var result in listSalesResultPPC)
                           {
                               if (result.effect.Equals(0))
                               {
                                   effect = result.effect;
                                   strMessage = result.strMessage;
                                   total_amount += result.GrandTotal;
                                   dtSalesDetail.Merge(result.subSalesDetail);
                                   dtSumExcessDigit.Merge(result.subSumExcessDigit);
                                   dtSumHotDigit.Merge(result.subSumHotDigit);
                                   dtSumHotDigitAgent.Merge(result.subSumHotDigitAgent);
                                   hot_updateSql.Append(result.strSumHotDigitUpdateSql);
                                   updateSql.Append(result.strSumExcessDigitUpdateSql);
                                   //sbRawData.Append(result.RawValue);//old
                                   arrRawData[result.salesIndex] = result.RawValue;//luo add Aug 28,2013
                                   if (!intExistForceInsert.Equals(1))
                                       intExistForceInsert = result.intExistForceInsert;
                                   if (customerBalance.Equals(0))
                                       customerBalance = result.customerBalance;
                                   if (custId.Equals(0))
                                       custId = result.custId;
                                   customerName = result.customer;
                               }
                               else
                               {
                                   effect = result.effect;
                                   strMessage = result.strMessage;
                                   break;
                                   //intExistForceInsert = result.intExistForceInsert;
                               }
                           }

if (Main.ctsDetail.IsCancellationRequested)
                                    {
                                        Main.countTaskRunTimesDetail = 0;
                                        Main.ctsDetail = new CancellationTokenSource();
                                        Task.Factory.StartNew(ProcessQueueDetail, Main.ctsDetail.Token);
                                    }
                                    if (Main.detailQueue == null)
                                        Main.detailQueue = new Queue<datatable>();
                                    Main.detailQueue.Enqueue(dtSalesDetail);//dtSalesDetail add to the detailQueue,waiting for ProcessQueueDetail()method deal

                                    #region summary hot & excess digit tasks
                                    if (dtSumHotDigit.Rows.Count > 0)
                                        Task_InsertSummaryHotDigit(dtSumHotDigit);

                                    if (dtSumHotDigitAgent.Rows.Count > 0)
                                        Task_InsertSummaryHotDigitAgent(dtSumHotDigitAgent);

                                    Task task3 = null;//this one is ok
                                    ctsExcessDigitInsert = new CancellationTokenSource();
                                    if (dtSumExcessDigit.Rows.Count > 0)
                                        task3 = Task.Factory.StartNew(() => Task_InsertSummaryExcessDigit(dtSumExcessDigit), ctsExcessDigitInsert.Token);

                                    if (hot_updateSql.ToString().Length > 0)
                                        Task_UpdateSummaryHotDigit(hot_updateSql.ToString());

                                    ctsExcessDigitUpdate = new CancellationTokenSource();
                                    Task task5 = null;//this one is ok
                                    if (updateSql.ToString().Length > 0)
                                        task5 = Task.Factory.StartNew(() => Task_UpdateSummaryExcessDigit(updateSql.ToString()), ctsExcessDigitUpdate.Token);
                                    #endregion
}

private salesResultPPC ppcManageShortSalesHelper(salesInfoPPC salesInfoppc)
{
return new salesResultPPC{}
}



目前有5位用户使用这项服务,所以任何人都可以提供帮助?

等待你。

thx。


Current have 5 users use this service,so anyone can help?
waiting for u.
thx.

推荐答案

Parallel.ForEach(sales, (salesInfoPPC) => listSalesResultPPC.Add(ppcManageShortSalesHelper(salesInfoPPC)));





You可以捕获 AggregateException 并使用它的 Handle 方法来了解问题所在。





You can catch the AggregateException and use it's Handle method to know what the problem was.

catch (AggregateException ae) {
    ae.Handle((x) =>
    {
        if (x is ArgumentNullException)
        {
           // manage the exception.
           return true; // do not stop the program
        }
        else if (x is UnauthorizedAccessException) 
        {
            // manage the access error.
            return true;
        }
        // else if any other exceptin type you want to handle. etc...

        else is (Exception ex)
        {
            // Any other exception here
        }
        return false; // Let anything else stop the application.
    });
}







Valery。




Valery.


这篇关于C#Parallel foreach - System.AggregateException:发生一个或多个错误。 ---&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 01:50