Search code examples
c#.netwpfvb.netbackgroundworker

Waiting for nested BackgroundWorkers to complete


I have a UI button that spawns a BackgroundWorker. This BackgroundWorker spawns several BackgroundWorkers that perform some operations. Is there a way to wait for all the inner BackgroundWorkers to complete in the main BackgroundWorker?


Solution

  • In your main worker code, you can check the IsBusy Property of the other workers

    "Gets a value indicating whether the BackgroundWorker is running an asynchronous operation."

    Private Sub BackgroundWorkerMaster_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorkerMaster.DoWork
        Do While bgwChild1.IsBusy AndAlso bgwChild2.IsBusy
            System.Threading.Thread.Sleep(1) 'wait for a small amount of time
        Loop
    End Sub