vb.net Code:
For each currentFarm in ... ... If urlQueue.Count > 0 Then Debug.Assert(currentFarm.isBusy = False) Debug.Assert(currentFarm.WebClient.IsBusy = False) ExecuteThread(urlQueue.Dequeue, currentFarm) 'currentFarm.PackedScannable = urlQueue.Dequeue 'currentFarm.Thread = New System.Threading.Thread(Sub() beginThreadAction.Invoke(currentFarm.PackedScannable, theFarm)) 'currentFarm.Thread.Start() 'webClientFarm.currentFarm.getURL(urlQueue.Dequeue) OneOfThemBusy = True ' this time the current farm must be busy because we just told them to grab a new URL right? so at least one of them is busy End If ... next currentFarm Public Shared Sub ExecuteThread(ByVal PackedScannableParameter As String, ByVal theFarm As enchancedWinClient) theFarm.PackedScannable = PackedScannableParameter theFarm.Thread = New System.Threading.Thread(Sub() beginThreadAction.Invoke(PackedScannableParameter, theFarm)) theFarm.Thread.Start() End Sub
The commented code won't work. That's because by the time the thread start, the variable currentFarm have changed.
So I create a new function and cache the currentFarm in the function parameter. No there will be a cache of currentFarm address (object is always a pointer) on a stack and that address is the one being passed to the System.Threading.Thread constructor.
Tada problem solved.




Reply With Quote