-1

Can somebody explain what path to use Paralle.Invoke or Parallel.ForEach in this scenario:

- query large data from database let's say 1000 records
- parallel process this data, save the records back to db, eventually insert new records etc

So the data will be returned like a List and then I have specific methods that will process certain records from the list based on a category property.

What should I use: .ForEach or .Invoke?

user2818430
  • 5,853
  • 21
  • 82
  • 148
  • 1
    It refers to processing, so.. what sort of processing do you have to do. – TheGeneral Aug 07 '18 at 10:26
  • Depends on what kind of `processing` you want to perform. Write the processing code in a standard `foreach`/`for` loop and compare that with `Parallel.ForEach`, see which one is faster. Also be careful with concurrent inserts and make sure the records are not coupled in any way. – undefined Aug 07 '18 at 10:29
  • Basically, for each record there will be some calculation done, some service calls, update that specific record and insert some new record in a completely different table. – user2818430 Aug 07 '18 at 10:30
  • Possible duplicate of [When to use a Parallel.ForEach loop instead of a regular foreach?](https://stackoverflow.com/q/12251874/1260204) (*see answer with the most number of votes*) – Igor Aug 07 '18 at 10:30
  • @Igor: What about Parallel.Invoke ? – user2818430 Aug 07 '18 at 10:33
  • 2
    You can process the data using whichever `Parallel` you like, but you must save it using the same thread that fetched it from EF. Your context isn't thread-safe. – Enigmativity Aug 07 '18 at 10:49

1 Answers1

-1

Several different tasks that have nothing to do with each other?

Parallel.Invoke

Several sequential tasks that need each others result?

Parallel.ForEach
Mel Gerats
  • 2,234
  • 1
  • 16
  • 33