Reputation: 364
This is an extension to the solution in 2 Async tasks in parallel and waiting for results - .Net
The methods of the two tasks are:
Private Sub tempWorker1()
For i = 1 To 50000
If i Mod 100 = 0 Then
Console.WriteLine("From 1:{0}", i)
End If
Next
End Sub
Private Sub tempWorker2()
For i = 1 To 1000
If i Mod 100 = 0 Then
Console.WriteLine("From 2:{0}", i)
End If
Next
End Sub
The calling methods are:
Dim task1 As Task = Task.Run(AddressOf tempWorker1)
Dim task2 As Task = Task.Run(AddressOf tempWorker2)
Await Task.WhenAll(task1, task2).ConfigureAwait(False)
Now, I'd need to have task1
and task2
run in parallel but task1
should not be over until task2
is complete. And the entire process should wait for both the tasks to complete.
Is there a way to achieve this using TPL. Or do I need to bank on global variables to stop task1
from finishing until task2
sets the global variable?
I am aware of ContinueWith
but that would defeat parallelism with the tasks running one after the other - which is not the requirement.
Upvotes: 0
Views: 68
Reputation: 456917
I'd need to have task1 and task2 run in parallel but task1 should not be over until task2 is complete.
So, just start task2
first, then pass task2
into tempWorker1
:
Dim task2 As Task = Task.Run(AddressOf tempWorker2)
Dim task1 As Task = Task.Run(Function() tempWorker1(task2))
And have tempWorker1
call Await task2
when it needs the value.
Upvotes: 1