Create a new thread in VB.NET

前端 未结 3 1739
粉色の甜心
粉色の甜心 2021-02-12 09:36

I am trying to create a new thread using an anonymous function but I keep getting errors. Here is my code:

New Thread(Function() 
    // Do something here
End Fu         


        
3条回答
  •  迷失自我
    2021-02-12 10:19

    It is called a lambda expression in VB. The syntax is all wrong, you need to actually declare a variable of type Thread to use the New operator. And the lambda you create must be a valid substitute for the argument you pass to the Thread class constructor. None of which take a delegate that return a value so you must use Sub, not Function. A random example:

    Imports System.Threading
    
    Module Module1
    
        Sub Main()
            Dim t As New Thread(Sub()
                                    Console.WriteLine("hello thread")
                                End Sub)
            t.Start()
            t.Join()
            Console.ReadLine()
        End Sub
    
    End Module
    

提交回复
热议问题