Search code examples
vb.netcontinuewith

Why does continueWith uses action(of task) as a parameter?


Basically, it asks for a sub with Task as a parameter. That's what Action(of Task) right?

Why?

I know I can pass normal sub to continueWith. I never remember passing a sub that requires a task parameter.


Solution

  • It is by the definition. 'ContinueWith' should in most cases operate with the result of 'antecedent' task. If you forget how to call 'ContinueWith', Visual Studio 'Peek Definition' will help you. So, right clicking on 'ContinueWith' and by choosing 'Peek Definition' you will examine the signature. Basically, it looks like is shown in the snippet below.

     public Task<TNewResult> ContinueWith<TNewResult>(
          Func<Task<TResult>, TNewResult> continuationFunction)
        {
          StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
          return this.ContinueWith<TNewResult>(continuationFunction, TaskScheduler.Current, new CancellationToken(), TaskContinuationOptions.None, ref stackMark);
        }
    

    If it is too complicated, you can use a snippet and save an example and then inserted when you need it.

    So, let's create an example.

    Module Module1
    
        Sub Main()
            Dim taskA As Task(Of DayOfWeek) = Task.Run(Function() DateTime.Today.DayOfWeek )
    
            ' Execute the continuation when the antecedent finishes.
            Dim taskB As Task(Of string) = taskA.ContinueWith(Function (antecedent)
                Return $"Today is {antecedent.Result}"
            End Function)
    
    
            taskb.Wait()
            Console.WriteLine(taskB.Result)
    
    
    
            Console.ReadLine()
        End Sub
    
    End Module