Create a new thread in VB.NET

前端 未结 3 1742
粉色の甜心
粉色の甜心 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:23

    There's two ways to do this;

    1. With the AddressOf operator to an existing method

      Sub MyBackgroundThread()
        Console.WriteLine("Hullo")
      End Sub
      

      And then create and start the thread with;

      Dim thread As New Thread(AddressOf MyBackgroundThread)
      thread.Start()
      
    2. Or as a lambda function.

      Dim thread as New Thread(
        Sub() 
          Console.WriteLine("Hullo")
        End Sub
      )
      thread.Start()
      

提交回复
热议问题