Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 寻找丑陋的多线程的标准替代方案_.net_Vb.net_Multithreading - Fatal编程技术网

.net 寻找丑陋的多线程的标准替代方案

.net 寻找丑陋的多线程的标准替代方案,.net,vb.net,multithreading,.net,Vb.net,Multithreading,我一直在尝试不同的异步处理数据的方法。我有一段代码可以在图像处理应用程序中完成这样的任务,但对我来说似乎很尴尬。我正在寻找符合当前标准的建议,或遵循的编码约定: ' this code is run on a background thread Dim lockThreadCounter As New Object() Dim runningThreadCounter As Integer = 0 Dim decrementCounterCallback As New AsyncCallback

我一直在尝试不同的异步处理数据的方法。我有一段代码可以在图像处理应用程序中完成这样的任务,但对我来说似乎很尴尬。我正在寻找符合当前标准的建议,或遵循的编码约定:

' 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
我考虑过并行。Foreach,但由于任务具有不同的代理示意图,因此无法提出使用它的解决方案。

您可以使用该类启动您的工作,并等待它们完成

这样就不需要“运行线程计数器”,因为每个任务都可以作为一个组存储和等待

这看起来像(一旦删除回调):


这正是我想要的。非常感谢。
Dim widths1 = Task.Factory.StartNew(Sub() widthsSlow())
Dim widths2 = Task.Factory.StartNew(Sub() widthsFast())
Dim bruteForce1 = Task.Factory.StartNew(Sub() numberOfSamples(pixelsSlow))
Dim bruteForce2 = Task.Factory.StartNew(Sub() numberOfSamples(pixelsFast))

' Wait for all to complete without the loop
Task.WaitAll(widths1, widths2, bruteForce1, bruteForce2)