user609926
user609926

Reputation: 831

TPL Parallel For Loop error

I'm getting a "one or more errors occurred" exception in my parallel.for loop:

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:\Users\FredWAD\Documents\Visual Studio 2010\Projects\PayrollCenterLibrary\BLL\ContributionFileManager.cs:line 218
   at myapp.staticlibrary.staticmethod(String str1, String str2) 

The app takes a collection of structs, each object containing metadata, and inserts them into a database.

The offending code is as follows:

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 = the collection object.

records = a List of structs containing metadata

fields = a struct within each record. each record contains a list of these.

This is essentially a table, with a struct for a row (which also contains some metadata about each row), and structs for cells. This error appears to be occurring at random. What am I doing wrong here?

Upvotes: 3

Views: 7298

Answers (2)

Jess
Jess

Reputation: 25069

One way to handle this is to put a try catch block within your Parallel loop. Handle the exception if you can inside the loop, so it does not break out with an AggregateException.

Upvotes: 0

theMayer
theMayer

Reputation: 16167

You will need to look at the InnerExceptions property on the AggregateException. This is the default behaviour for TPL as multiple threads could throw exceptions at the same time.

See http://msdn.microsoft.com/en-us/library/system.aggregateexception.aspx for more information.

My guess is that you have some type of resource that is not thread-safe; when parallel threads are accessing it, the resource gets into a race condition with another thread, resulting in your exception. The first task is to figure out which line of your parallel query is causing the problem, the go into that. It likely has something to do with the database if your database level row locks are not adequate.

Upvotes: 4

Related Questions