1

I am encountering a problem where the "WaitAll" for my tasks does not wait until all of the tasks are finished. This is a VB project with sub's and functions. A lot of the other examples of this (in C#) use lambda's that do not easily convert to VB.

I have tested with a sample program and gotten it to work, but as soon as I try to communicate with a web service (which adds async and await), it does not wait at the WaitAll anymore.

This is an example of the log file. You can see how the button click ends on lines 6 and 7, even though the tasks are still running and continue to log work after the button click has finished.

2018-07-05 12:18:38.1481 |  INFO | Form1.btnRun_Click | ############################ 
2018-07-05 12:18:38.1790 |  INFO | Form1.btnRun_Click | Run button clicked 
2018-07-05 12:18:38.2129 |  INFO | ProcessOneChunkOfPeopleData | Thread #1 contains 2 people 
2018-07-05 12:18:38.2129 |  INFO | ProcessOneChunkOfPeopleData | Thread #2 contains 2 people 
2018-07-05 12:18:38.2129 |  INFO | ProcessOneChunkOfPeopleData | Thread #3 contains 2 people 
2018-07-05 12:18:38.2980 |  INFO | Form1.btnRun_Click | Run button code has completed 
2018-07-05 12:18:38.2980 |  INFO | Form1.btnRun_Click | ############################ 
2018-07-05 12:18:38.3788 |  INFO | ProcessOneChunkOfPeopleData | Thread 1. Finished processing person Peter (1 of 2) 
2018-07-05 12:18:38.3788 |  INFO | ProcessOneChunkOfPeopleData | Thread 3. Finished processing person Dave (1 of 2) 
2018-07-05 12:18:38.3947 |  INFO | ProcessOneChunkOfPeopleData | Thread 2. Finished processing person Chantal (1 of 2) 
2018-07-05 12:18:38.4665 |  INFO | ProcessOneChunkOfPeopleData | Thread 3. Finished processing person Helen (2 of 2) 
2018-07-05 12:18:38.4695 |  INFO | ProcessOneChunkOfPeopleData | Thread #3 has finished processing all people 
2018-07-05 12:18:38.4948 |  INFO | ProcessOneChunkOfPeopleData | Thread 2. Finished processing person Tammy (2 of 2) 
2018-07-05 12:18:38.5018 |  INFO | ProcessOneChunkOfPeopleData | Thread #2 has finished processing all people 
2018-07-05 12:18:38.4948 |  INFO | ProcessOneChunkOfPeopleData | Thread 1. Finished processing person Jeff (2 of 2) 
2018-07-05 12:18:38.5157 |  INFO | ProcessOneChunkOfPeopleData | Thread #1 has finished processing all people 

Here is the code I have been testing with. Can you help explain why the WaitAll does not wait?

Imports System.Net.Http
Imports NLog

Public Class Form1

    Private m_Logger As Logger = LogManager.GetCurrentClassLogger

    Private Sub btnRun_Click(sender As Object, e As EventArgs) Handles btnRun.Click
        m_Logger.Info("############################")
        m_Logger.Info("Run button clicked")

        'Create a bunch of people
        Dim Peter As New Person(1, "Peter")
        Dim Jeff As New Person(2, "Jeff")
        Dim Chantal As New Person(3, "Chantal")
        Dim Tammy As New Person(4, "Tammy")
        Dim Dave As New Person(5, "Dave")
        Dim Helen As New Person(6, "Helen")

        'Load people into groups
        Dim Group1 As New List(Of Person)
        Group1.Add(Peter)
        Group1.Add(Jeff)
        Dim Group2 As New List(Of Person)
        Group2.Add(Chantal)
        Group2.Add(Tammy)
        Dim Group3 As New List(Of Person)
        Group3.Add(Dave)
        Group3.Add(Helen)

        'Create a list of lists. Each list will be processed as a separate task
        Dim MySplitList As New List(Of List(Of Person))
        MySplitList.Add(Group1)
        MySplitList.Add(Group2)
        MySplitList.Add(Group3)

        'Create an array of 3 tasks
        Dim myTasks(2) As Task

        'Load the task array with what work it needs to do
        Dim iThreadNumber As Integer = 1
        For Each OneList As List(Of Person) In MySplitList
            Dim iNEW As Integer = iThreadNumber

            myTasks(iNEW - 1) = System.Threading.Tasks.Task.Run(Sub() ProcessOneChunkOfPeopleData(OneList, iNEW))

            iThreadNumber += 1
        Next OneList

        'Wait for all tasks to complete.
        Task.WaitAll(myTasks)

        m_Logger.Info("Run button code has completed")
        m_Logger.Info("############################")

        MessageBox.Show(Me, "Process complete", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Async Sub ProcessOneChunkOfPeopleData(ByVal arPeople As List(Of Person), ByVal iSectionNumber As Integer)
        Try
            m_Logger.Info(String.Format("Thread #{0} contains {1} people", iSectionNumber, arPeople.Count))

            Dim iCounter As Integer = 1

            For Each xOnePerson As Person In arPeople
                'Simulate a call to a web service
                Dim bWorkedOkay1 As Boolean = Await FakeApiCall()

                m_Logger.Info(String.Format("Thread {0}. Finished processing person {1} ({2} of {3})", iSectionNumber, xOnePerson.Name, iCounter, arPeople.Count))

                iCounter += 1
            Next xOnePerson

            m_Logger.Info(String.Format("Thread #{0} has finished processing all people", iSectionNumber))
        Catch Exp As Exception
            m_Logger.Error(Exp.Message)
        End Try
    End Sub

    Public Async Function FakeApiCall() As Task(Of Boolean)
        Dim http As New HttpClient
        'This is the API which is causing the task WaitAll to not wait
        Dim xResponse As HttpResponseMessage = Await http.GetAsync("https://jsonplaceholder.typicode.com/posts/1")
        Dim sResult As String = Await xResponse.Content.ReadAsStringAsync()
        Return True
    End Function

End Class

Public Class Person
    Public Property Name As String = ""
    Public Property ID As Integer = 0

    Public Sub New()
        '
    End Sub

    Public Sub New(ByVal iID As Integer, ByVal sName As String)
        Me.ID = iID
        Me.Name = sName
    End Sub
End Class
ThePeter
  • 883
  • 1
  • 7
  • 15

1 Answers1

0

I cannot explain why the code works when you replace the API call with a sleep. I can offer this as a solution to fix the code and make it wait for the WaitAll to complete.

Below are the three changes you need to do in order to make the project work. The changes are done to make the Sub into a function – so it is now forced to return something.

In btnRun_Click, change

myTasks(iNEW - 1) = System.Threading.Tasks.Task.Run(Sub() ProcessOneChunkOfPeopleData(OneList, iNEW))

to be

myTasks(iNEW - 1) = System.Threading.Tasks.Task.Run(Function() ProcessOneChunkOfPeopleData(OneList, iNEW))

Change function ProcessOneChunkOfPeopleData from this

Private Async Sub ProcessOneChunkOfPeopleData(ByVal arPeople As List(Of Person), ByVal iSectionNumber As Integer)

to this

Private Async Function ProcessOneChunkOfPeopleData(ByVal arPeople As List(Of Person), ByVal iSectionNumber As Integer) As Task(Of Boolean)

Add this to the very last line of ProcessOneChunkOfPeopleData

Return True
ThePeter
  • 883
  • 1
  • 7
  • 15