Parallel.ForEach(files, (FileInfo datafile, ParallelLoopState state) => { if (errorCount > maxErrors) { iterationMaxErrorAchieved = true; state.Stop(); } else { ImportDataProcess(datafile); } });
void ImportPolicyInternalProcess(FileInfo datafile) { try { XmlDocument xmldoc = new XmlDocument(); using (FileStream stream = datafile.OpenRead()) { xmldoc.Load(stream); } //....some code... if (InsertData(xmldoc.DocumentElement, int value1ID, int value2ID)) { } processedPolicyFileCounter++; } catch (Exception E) { } }
Typically, you can make your method thread-safe, by wrapping the critical sections (the code that use the common resource) with a lock statement. In your case, it seems like the critical section is the DB access in the InsertData method.
InsertData
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)