Return once condition is true without hanging

Viewed 48

So I have this computation job, that requires startng 6 threads and waiting for those to finish. The threads change a "local" variable within the class. I want to have the function return "True" once the local variable is a certain value. However, I want to do this in a fashion where it doesn't hang the thread. So a constant "Do Loop" is not gonna work. Are there any standard ways of doing 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
1 Answers
Related