Hi everyone and thanks for reading..

I am getting myself mixed up while thinking about chaining multiple methods together using a single task.

Okay, so, I am passing multiple methods and their respective argument (always a single object) into a collection. When all the methods I want to run have been added to the collection, I run the methods in turn using a single task for all of the methods in the collection.

So for example, I am able to run the methods in turn by using the following:

VB.NET Code:
  1. 'Me.Token is a CancellationToken used to exit the task if a cancellation was requested.
  2. 'Item is a Class with two properties [.Method is an Action(Of Object) and .Argument is an Object]
  3. Return Task.Factory.StartNew(
  4.     Sub()
  5.         Me.Token.ThrowIfCancellationRequested()
  6.  
  7.         For Each Item As TaskItem In Me.List
  8.             Task.Factory.StartNew(Item.Method, Item.Argument, Me.Token, TaskCreationOptions.AttachedToParent, TaskScheduler.Current)
  9.         Next Item
  10.     End Sub, Me.Token)

I would now like to extend on this and to run a series of Functions in a chained task and to collect the results of each function within that chain. My understanding is that you can pass the result of one function to the parameter of the next by using the ContinueWith method however to also collect the results I think I would need to do this with the use of a Task(Of TResult).

So what I would like to know is - if I start with the following (which will run the first function from within my collection and collect the result):

VB.NET Code:
  1. Dim T As Task(Of Object) = Task(Of Object).Factory.StartNew(Me.List.Item(0).Method, Me.List.Item(0).Argument, Me.Token, TaskCreationOptions.AttachedToParent, TaskScheduler.Current)

What do I need to do to allow ContinueWith to run each of my remaining functions while passing the TResult of the previous function to the next.
VB.NET Code:
  1. For iChild As Integer = 1 To Me.Count - 1
  2.     ' What should go here.
  3.     T = T.Factory.ContinueWith...
  4. Next iChild
I am really struggling to understand the MSDN documentation on this (it wants a func(of object, object) for the first parameter of the ContinueWith method and I am not sure how to go about that). I cannot find an example close enough to what I am doing in order to follow it.


I hope I have made sense, but let me know if I need to be any clearer.

Cheers,
Jay