A task factory is represented by the System.Threading.Tasks.TaskFactory class, which creates Task objects, or the System.Threading.Tasks.TaskFactory(Of TResult) class, which creates Task(Of TResult) objects. Both classes contain methods that you can use to:
Create tasks and start them immediately. For more information, see Task Parallelism (Task Parallel Library).
Create task continuations that start when any or all of an array of tasks complete. For more information, see Continuation Tasks.
Create tasks that represent pairs of begin/end methods that follow the Asynchronous Programming Model. For more information, see TPL and Traditional .NET Framework Asynchronous Programming.
Dim taskA as Task = Task.Factory.StartNew(Sub( ...))
Task taskA = Task.Factory.StartNew( () => ...);
In most scenarios, you do not have to derive a new class from TaskFactory. However, it is sometimes useful to configure a new TaskFactory and then use it to specify certain options or to associate tasks with a custom scheduler. The following example shows how to configure a new TaskFactory that creates tasks that all use the specified TaskScheduler and have the specified TaskCreationOptions options.
Class Program Shared Sub Main() Dim cts As CancellationTokenSource = New CancellationTokenSource() Dim _factory As TaskFactory = New TaskFactory( cts.Token, TaskCreationOptions.PreferFairness, TaskContinuationOptions.ExecuteSynchronously, New MyScheduler()) Dim t2 = _factory.StartNew(Sub() DoWork()) End Sub Shared Sub DoWork() ' ... End Sub