Parallel foreach hanging on completions

  • Thread starter Thread starter Cheesebread
  • Start date Start date
C

Cheesebread

Guest
Good evening

I have a .net core application that I am using DirectorySearcher to loop through to get all the information for AD. The original app did this on a single thread and took hours. I broke up the search into wildcards by first letter (a-z plus some special characters) and implemented a Parallel.ForEach on the array of characters using -1 as the MaxDegreeOfParallelism. This has sped up the fetch and subsequent writing of results to SQL Server tremendously. Unfortunately I am seeing on the last iteration the code hangs about 50% of the time. My console.writeline shows the last part of the loop completes but doesn't move on. I threw a counter and a test for counter == array length then attempted to apply a Break() but it didn't improve. Any suggestions would be appreciated.

int[] charList = new int[]{33,34,35,36...}

int counter =0;

var options - new ParallelOptions()

{

MaxDegreeOf Parallelism = -1;

}

Parallel.ForEach(charList,options(item,loopState=>

{

//create filter and call directory searcher and insert results to staging table 1st user then group


counter++;

if(counter == charList.Length){

loopState.Break();

}

});


Thanks.

Continue reading...
 
Back
Top