我一直在尝试不同的方法来异步处理数据。我有一段代码可以在图像处理应用程序中完成这样的任务,但对我来说似乎很尴尬。我正在寻找符合当前标准的建议,或要遵循的编码约定:
' this code is run on a background thread
Dim lockThreadCounter As New Object()
Dim runningThreadCounter As Integer = 0
Dim decrementCounterCallback As New AsyncCallback(
Sub()
SyncLock lockThreadCounter
runningThreadCounter -= 1
End SyncLock
End Sub)
runningThreadCounter += 1
widthsAdder.BeginInvoke(widthsSlow, decrementCounterCallback, Nothing)
runningThreadCounter += 1
widthsAdder.BeginInvoke(widthsFast, decrementCounterCallback, Nothing)
runningThreadCounter += 1
bruteForceCalculateR2.BeginInvoke(numberOfSamples, pixelsSlow, decrementCounterCallback, Nothing)
runningThreadCounter += 1
bruteForceCalculateR2.BeginInvoke(numberOfSamples, pixelsFast, decrementCounterCallback, Nothing)
' wait here for all four tasks to complete
While runningThreadCounter > 0
Thread.Sleep(1)
End While
' resume with the rest of the code once all four tasks have completed
我考虑过Parallel.Foreach
但无法提出使用它的解决方案,因为任务具有不同的委托足迹。