parellel.for循环 - 为什么不抛出错误

时间:2015-08-11 16:16:44

标签: vb.net visual-studio-2012

我刚刚将“CODE 1”转换为“CODE 2”中使用“parellel.for”循环但为什么“CODE 2”没有正确运行?

  

错误:“AmbiguousMatchExceptionUnhandled”

代码1

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button2.Click

        Dim sum As Integer = 0
        For i As Integer = 0 To 99

            sum = sum + i
        Next
        TextBox1.Text = sum

    End Sub
End Class

代码2

Imports System.Threading
Imports System.Threading.Tasks

Public Class Form1
    Dim i As Integer = 0


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Parallel.For(i, 99, k)

    End Sub


    Function k()

        Dim sum As Integer = 0
        For i As Integer = 0 To 99

            sum = sum + i
        Next
        TextBox1.Text = sum

        Return Nothing
    End Function


End Class

1 个答案:

答案 0 :(得分:0)

由于Function k()不接受Integer参数,因此它是Parallel.For的不合适代理。

现在,想一想为什么函数需要参数,然后考虑为什么你不会得到你期望的结果。即使你解决了这个第一个肤浅的问题。

并行意味着循环的所有循环可以同时运行。