Matt9Atkins Matt9Atkins - 2 years ago 111 Question

Create a new thread in VB.NET

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 Function).Start

Here are the errors I get:


Syntax Error

End Function:

'End Function' must be preceded by a matching 'Function'.

Answer Source

There's two ways to do this;

  1. With the AddressOf operator to an existing method

    Sub MyBackgroundThread()
    End Sub

    And then create and start the thread with;

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

    Dim thread as New Thread(
      End Sub
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download