Imports System.ComponentModelPublic ClassForm1'lists of workersand proxies Dim workers AsList(Of BackgroundWorker) = NewList(Of BackgroundWorker) Dim proxies AsList(Of String) Private SubForm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load 'fill list ofworkers workers.Add(BackgroundWorker1) workers.Add(BackgroundWorker2) 'here filllist of proxies 'first run:starting all background workers For Each worker Inworkers Ifproxies.Count > 0 Then worker.RunWorkerAsync(proxies(0)) proxies.RemoveAt(0) EndIf Next End Sub 'this sub would be called each time workercompletes current task Sub queue() Ifproxies.Count > 0 Then 'if there are items left in proxies list ForEach worker Inworkers 'iterate workers Ifworker.IsBusy = False Then'if free worker is found, give him the next task worker.RunWorkerAsync(proxies(0)) proxies.RemoveAt(0) ExitSub 'end sub, the nex completed task wouldcall it again EndIf Next Else 'if there are no proxies to process ForEach worker Inworkers 'iterate workers Ifworker.IsBusy = True Then'if busy worker is found, exit sub, it woul be calledby next completed task ExitSub EndIf Next MsgBox("Allis done") 'the messagebox would be calledonly if all workers are free End If End Sub 'binding queue sub for work completed event Private SubBackgroundWorker1_RunWorkerCompleted(ByValsender As Object,ByVal e AsSystem.ComponentModel.RunWorkerCompletedEventArgs)Handles BackgroundWorker1.RunWorkerCompleted queue() End Sub Private SubBackgroundWorker2_RunWorkerCompleted(ByValsender As Object,ByVal e AsSystem.ComponentModel.RunWorkerCompletedEventArgs)Handles BackgroundWorker2.RunWorkerCompleted queue() End SubEnd Class
Comments