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:
New:
Syntax Error
End Function:
'End Function' must be preceded by a matching 'Function'.
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:
There's two ways to do this;
With the
AddressOf
operator to an existing methodAnd then create and start the thread with;
Or as a lambda function.
What is called has to be a functinon not a sub.
Single line(has to return value):
Multiline:
Source: Threading, Closures, and Lambda Expressions in VB.Net