我在我的并行中遇到"一个或多个错误"。循环:
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.Wait()
at System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Func`4 bodyWithLocal, Func`1 localInit, Action`1 localFinally)
at System.Threading.Tasks.Parallel.For(Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action`1 body)
at StaticClassLibrary.BLL.StaticClass.StatiMethod(String strExt, Object wTable, Object job, String BSPConnectionString) in c:UsersFredWADDocumentsVisual Studio 2010ProjectsPayrollCenterLibraryBLLContributionFileManager.cs:line 218
at myapp.staticlibrary.staticmethod(String str1, String str2)
该应用程序收集了一个结构集,每个对象包含元数据,然后将它们插入数据库中。
有问题的代码如下:
Parallel.For(0, recordCnt, pOptions, d =>
{
//flds = wTable.records[d].fields;
ssn = wTable.records[d].fields[fieldIndex].Value;
//rowId = wTable.records[d].fields[fieldIndex].rowId;
currentPerson = PersontManager.GetPerson(string1, string2);
hasContributions = WorkTableManager.RowHasContributionsNEW(List<string> lst, wTable.records[d]);
LoadRecordParallel(hasLoan, hasScratchpad, fieldIndex, wTable.records[d], object, string, string);
}
);
wtable =收集对象。
记录=包含元数据的结构列表
字段=每个记录中的一个结构。每个记录都包含其中的列表。
这本质上是一张表格,有一个用于行的结构(还包含有关每一行的一些元数据),并且是单元的结构。此错误似乎是随机发生的。我在这里做错了什么?
您将需要查看gentregateException上的Innerexceptions属性。这是TPL的默认行为,因为多个线程可能同时投掷异常。
请参阅http://msdn.microsoft.com/en-us/library/system.aggregateexception.aspx有关更多信息。
我的猜测是您有某种不是线程安全的资源。当并行线程访问它时,资源将带有另一个线程的种族条件,从而导致您的异常。第一个任务是找出您的并行查询的哪一行引起的问题,请参与其中。如果您的数据库级别锁定不足,则可能与数据库有关。
处理此操作的一种方法是将try
catch
块放入并行循环中。如果您可以在循环内部处理异常,因此不会使用AggregateException
爆发。