2

I'm attempting to Parallelize the following For Each loop which works as expected. I started with this:

foreach (DataRow drGroup in dsGroups.Tables["Table"].Rows)
         ProduceInvoices(drGroup);

and changed it to:

Parallel.ForEach<DataRow>((IEnumerable<DataRow>)dsGroups.Tables["Table"].Rows, ProduceInvoices)

however ProduceInvoices seem to no longer get executed, despite dsGroup containing rows.

Please can you provide me with any pointers and/or where to look?

m.edmondson
  • 30,382
  • 27
  • 123
  • 206
  • You should have had some kind of invalid cast exception thrown, do you have a empty `catch` somewhere that was swallowing the error? Also turning on [breaking on handled exceptions](http://stackoverflow.com/questions/116896/visual-studio-how-to-break-on-handled-exceptions) can also be helpful in tracking this kind of stuff down. – Scott Chamberlain Sep 09 '14 at 15:11
  • @ScottChamberlain - It's a really poorly written legacy app so more than likely has the odd empty `catch`, otherwise would've spotted this. Next time I'll try and break on handled exceptions, thanks. – m.edmondson Sep 09 '14 at 15:33

1 Answers1

7

Try using AsEnumerable method:

Parallel.ForEach<DataRow>(dsGroups.Tables["Table"].AsEnumerable(), ProduceInvoices);

Rows property returns a DataRowCollection which doesn't implement IEnumerable<T>.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184