asp.net mvc 4 - Parallel Foreach Race Condition -


i have issue parallel foreach closing connection before finishes executing. when had regular foreach loop runnung slow return everything. once changed parallel foreach returning 95% of data , terminating.

below code using:

var uspostalcodes = repository.getuspostalcodes();                 var capostalcodes = repository.getcapostalcodes();                  parallel.foreach(spreadsheetinfo, location =>                 {                      locationdata locationdata = new locationdata()                     {                         id = location.id,                         market = repository.getmarketsforpostalcode(location.postalcode, uploadedfile, uspostalcodes, capostalcodes),                     };                      locationlist.add(locationdata);                 });  added following code check , see going on, , fixed returning rows know race condition exists can't figure out why , how ot fix it.  suggestions appreciated  console.writeline("processing {0} on thread {1}", locationdata,                                     thread.currentthread.managedthreadid); 

locationlist isn't thread-safe. therefore, you're corrupting list.

instead, should use .asparrelel.select() run delegates in parallel , return ienumerable<t> results.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -