.net启动变量函数作为线程

时间:2016-05-23 08:48:00

标签: .net vb.net multithreading

如何启动作为变量传递的函数作为.net 3.5中的线程?

Public Shared Sub starteAlsThread2(addr As Threading.ParameterizedThreadStart)
    Dim pts As New Threading.ParameterizedThreadStart(addr)
    lgv_worker = New Threading.Thread(pts)
    lgv_worker.Start(Argumente)
End Sub

这种方式存在编译错误: " System.Threading.ParameterizedThreadStart"需要addressof或lambda表达式

1 个答案:

答案 0 :(得分:-1)

使用lambda表达式可以轻松地将变量传递给新线程:

    Private Sub Test()
        Dim thrd = New Thread(Sub() TestSub("bllablla"))
        thrd.Start()
    End Sub

    Private Sub TestSub(kot As String)
        'Do stuff here
    End Sub