Return when condition is true, no hang

So I have this computation job that requires starting 6 threads and waiting for them to complete. Threads change the "local" variable within the class. I want the function to return "True" when the local variable is a specific value. However, I want to make it so that it doesn't hang the thread. Therefore the constant "Do Loop" will not work. Are there standard ways to do this?

Public Function Start(ByVal Cores As Integer) As Boolean
    For i = 0 To 10
      // Heavy work

        Task.Factory.StartNew(Sub() Compute(Core, StartInt, EndInt))

    Next

    Do // <- How to avoid checking ThreadsTerimnated = ThreadsStarted every clockcycle?
       // Threading.Sleep hangs thread.

        If ThreadsTerminated = ThreadsStarted Then
            MergeResults(Cores)
            Return True
        End If
    Loop

End Function

      

+3


source to share


1 answer


You can save the list Tasks

and useTasks.WaitAll



Dim tasks As New List(Of Task)
For i = 0 To 10
  // Heavy work
    tasks.Add(Task.Factory.StartNew(Sub() Compute(Core, StartInt, EndInt)))
Next

Task.WaitAll(tasks.ToArray())

      

+4


source







All Articles