My current solution uses the ThreadPool to process transactions. Every couple minutes I grab 1-200 transactions and queue each one via the QueueUserWorkItem function. Something like this where ‘trans’ is my collection of transactions:
For Each t As ManagerTransaction In trans
Threading.ThreadPool.QueueUserWorkItem(AddressOf ProcessManagerTransaction, t)
Next
I want to switch it over to use the TPL, however, after much research I am still unsure of the best way to go about it. I have the following options yet I haven’t been able to find a general consensus on what the best practice is.
1) Threading.Tasks.Parallel.ForEach(trans, AddressOf ProcessManagerTransaction)
Where “t” is an individual transaction in my “trans” collection
2) Task.Factory.StartNew(AddressOf ProcessManagerTransaction, t)
2a) Task.Factory.StartNew(Sub() ProcessManagerTransaction(t)
And this combination of the two:
3) Task.Factory.StartNew(Function() Parallel.ForEach(trans, AddressOf ProcessManagerTransaction))
The first option is generally perferrable because it does everything you want: parallelization and propagation of errors. Options 2 and 3 require additional means to propagate errors.
Option 2 might come into play if you require having tasks so you can compose them with other tasks.
I do not really see a case where I would use option 3.