I am trying to start a new thread in VB.NET and I’m having syntax problems.
In C# this is how we can do the same task using:
var manualResetEvent = new ManualResetEvent(false);
waitHandles.Add(manualResetEvent);
var taskOne = Task.Factory.StartNew(() => new Thread(TaskToRun).Start(manualResetEvent));
taskOne.Wait();
TaskToRun is a void method with a single object parameter.
All online converters generate VB.NET code similar to this:
Dim manualResetEvent = New ManualResetEvent(False)
waitHandles.Add(manualResetEvent)
Dim taskOne = Task.Factory.StartNew(Function() New Thread(AddressOf TaskToRun).Start(manualResetEvent))
taskOne.Wait()
There is no reason to start a Task that does nothing but launch a Thread. You should just create the Task directly. You should be able to do that in VB via:
If you want to force the Task to use a dedicated thread, you can do so by passing the LongRunning hint. With the default TaskScheduler, this will start the task on a dedicated thread (instead of the ThreadPool).
For reference, the equivelent C# would be: