Export (0) Print
Expand All

TaskFactory(Of TResult) Class

Provides support for creating and scheduling Task(Of TResult) objects.

System.Object
  System.Threading.Tasks.TaskFactory(Of TResult)

Namespace:  System.Threading.Tasks
Assemblies:   mscorlib (in mscorlib.dll)
  System.Threading.Tasks (in System.Threading.Tasks.dll)

'Declaration
<HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization := True,  _
	ExternalThreading := True)> _
Public Class TaskFactory(Of TResult)

Type Parameters

TResult

The return value of the Task(Of TResult) objects that the methods of this class create.

The TaskFactory(Of TResult) type exposes the following members.

  NameDescription
Public methodSupported by Portable Class LibraryTaskFactory(Of TResult)Initializes a TaskFactory(Of TResult) instance with the default configuration.
Public methodSupported by Portable Class LibraryTaskFactory(Of TResult)(CancellationToken)Initializes a TaskFactory(Of TResult) instance with the default configuration.
Public methodSupported by Portable Class LibraryTaskFactory(Of TResult)(TaskScheduler)Initializes a TaskFactory(Of TResult) instance with the specified configuration.
Public methodSupported by Portable Class LibraryTaskFactory(Of TResult)(TaskCreationOptions, TaskContinuationOptions)Initializes a TaskFactory(Of TResult) instance with the specified configuration.
Public methodSupported by Portable Class LibraryTaskFactory(Of TResult)(CancellationToken, TaskCreationOptions, TaskContinuationOptions, TaskScheduler)Initializes a TaskFactory(Of TResult) instance with the specified configuration.
Top

  NameDescription
Public propertySupported by Portable Class LibraryCancellationTokenGets the default cancellation token for this task factory.
Public propertySupported by Portable Class LibraryContinuationOptionsGets the TaskContinuationOptions enumeration value for this task factory.
Public propertySupported by Portable Class LibraryCreationOptionsGets the TaskCreationOptions enumeration value for this task factory.
Public propertySupported by Portable Class LibrarySchedulerGets the task scheduler for this task factory.
Top

  NameDescription
Public methodSupported by Portable Class LibraryContinueWhenAll(Task(), Func(Of Task(), TResult))Creates a continuation task that will be started upon the completion of a set of provided tasks.
Public methodSupported by Portable Class LibraryContinueWhenAll(Task(), Func(Of Task(), TResult), CancellationToken)Creates a continuation task that will be started upon the completion of a set of provided tasks.
Public methodSupported by Portable Class LibraryContinueWhenAll(Task(), Func(Of Task(), TResult), TaskContinuationOptions)Creates a continuation task that will be started upon the completion of a set of provided Tasks.
Public methodSupported by Portable Class LibraryContinueWhenAll(Task(), Func(Of Task(), TResult), CancellationToken, TaskContinuationOptions, TaskScheduler)Creates a continuation task that will be started upon the completion of a set of provided Tasks.
Public methodSupported by Portable Class LibraryContinueWhenAll(Of TAntecedentResult)(Task(Of TAntecedentResult)(), Func(Of Task(Of TAntecedentResult)(), TResult))Creates a continuation task that will be started upon the completion of a set of provided tasks.
Public methodSupported by Portable Class LibraryContinueWhenAll(Of TAntecedentResult)(Task(Of TAntecedentResult)(), Func(Of Task(Of TAntecedentResult)(), TResult), CancellationToken)Creates a continuation task that will be started upon the completion of a set of provided tasks.
Public methodSupported by Portable Class LibraryContinueWhenAll(Of TAntecedentResult)(Task(Of TAntecedentResult)(), Func(Of Task(Of TAntecedentResult)(), TResult), TaskContinuationOptions)Creates a continuation task that will be started upon the completion of a set of provided tasks.
Public methodSupported by Portable Class LibraryContinueWhenAll(Of TAntecedentResult)(Task(Of TAntecedentResult)(), Func(Of Task(Of TAntecedentResult)(), TResult), CancellationToken, TaskContinuationOptions, TaskScheduler)Creates a continuation task that will be started upon the completion of a set of provided tasks.
Public methodSupported by Portable Class LibraryContinueWhenAny(Task(), Func(Of Task, TResult))Creates a continuation task that will be started upon the completion of any task in the provided set.
Public methodSupported by Portable Class LibraryContinueWhenAny(Task(), Func(Of Task, TResult), CancellationToken)Creates a continuation task that will be started upon the completion of any task in the provided set.
Public methodSupported by Portable Class LibraryContinueWhenAny(Task(), Func(Of Task, TResult), TaskContinuationOptions)Creates a continuation task that will be started upon the completion of any task in the provided set.
Public methodSupported by Portable Class LibraryContinueWhenAny(Task(), Func(Of Task, TResult), CancellationToken, TaskContinuationOptions, TaskScheduler)Creates a continuation task that will be started upon the completion of any task in the provided set.
Public methodSupported by Portable Class LibraryContinueWhenAny(Of TAntecedentResult)(Task(Of TAntecedentResult)(), Func(Of Task(Of TAntecedentResult), TResult))Creates a continuation task that will be started upon the completion of any task in the provided set.
Public methodSupported by Portable Class LibraryContinueWhenAny(Of TAntecedentResult)(Task(Of TAntecedentResult)(), Func(Of Task(Of TAntecedentResult), TResult), CancellationToken)Creates a continuation task that will be started upon the completion of any task in the provided set.
Public methodSupported by Portable Class LibraryContinueWhenAny(Of TAntecedentResult)(Task(Of TAntecedentResult)(), Func(Of Task(Of TAntecedentResult), TResult), TaskContinuationOptions)Creates a continuation task that will be started upon the completion of any task in the provided set.
Public methodSupported by Portable Class LibraryContinueWhenAny(Of TAntecedentResult)(Task(Of TAntecedentResult)(), Func(Of Task(Of TAntecedentResult), TResult), CancellationToken, TaskContinuationOptions, TaskScheduler)Creates a continuation task that will be started upon the completion of any task in the provided set.
Public methodSupported by Portable Class LibraryEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodSupported by Portable Class LibraryFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Portable Class LibraryFromAsync(IAsyncResult, Func(Of IAsyncResult, TResult))Creates a task that executes an end method function when a specified IAsyncResult completes.
Public methodSupported by Portable Class LibraryFromAsync(Func(Of AsyncCallback, Object, IAsyncResult), Func(Of IAsyncResult, TResult), Object)Creates a task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.
Public methodSupported by Portable Class LibraryFromAsync(IAsyncResult, Func(Of IAsyncResult, TResult), TaskCreationOptions)Creates a task that executes an end method function when a specified IAsyncResult completes.
Public methodSupported by Portable Class LibraryFromAsync(Func(Of AsyncCallback, Object, IAsyncResult), Func(Of IAsyncResult, TResult), Object, TaskCreationOptions)Creates a task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.
Public methodSupported by Portable Class LibraryFromAsync(IAsyncResult, Func(Of IAsyncResult, TResult), TaskCreationOptions, TaskScheduler)Creates a task that executes an end method function when a specified IAsyncResult completes.
Public methodSupported by Portable Class LibraryFromAsync(Of TArg1)(Func(Of TArg1, AsyncCallback, Object, IAsyncResult), Func(Of IAsyncResult, TResult), TArg1, Object)Creates a task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.
Public methodSupported by Portable Class LibraryFromAsync(Of TArg1)(Func(Of TArg1, AsyncCallback, Object, IAsyncResult), Func(Of IAsyncResult, TResult), TArg1, Object, TaskCreationOptions)Creates a task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.
Public methodSupported by Portable Class LibraryFromAsync(Of TArg1, TArg2)(Func(Of TArg1, TArg2, AsyncCallback, Object, IAsyncResult), Func(Of IAsyncResult, TResult), TArg1, TArg2, Object)Creates a task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.
Public methodSupported by Portable Class LibraryFromAsync(Of TArg1, TArg2)(Func(Of TArg1, TArg2, AsyncCallback, Object, IAsyncResult), Func(Of IAsyncResult, TResult), TArg1, TArg2, Object, TaskCreationOptions)Creates a task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.
Public methodSupported by Portable Class LibraryFromAsync(Of TArg1, TArg2, TArg3)(Func(Of TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult), Func(Of IAsyncResult, TResult), TArg1, TArg2, TArg3, Object)Creates a task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.
Public methodSupported by Portable Class LibraryFromAsync(Of TArg1, TArg2, TArg3)(Func(Of TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult), Func(Of IAsyncResult, TResult), TArg1, TArg2, TArg3, Object, TaskCreationOptions)Creates a task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.
Public methodSupported by Portable Class LibraryGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodSupported by Portable Class LibraryGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Portable Class LibraryMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Portable Class LibraryStartNew(Func(Of TResult))Creates and starts a task.
Public methodSupported by Portable Class LibraryStartNew(Func(Of TResult), CancellationToken)Creates and starts a task.
Public methodSupported by Portable Class LibraryStartNew(Func(Of TResult), TaskCreationOptions)Creates and starts a task.
Public methodSupported by Portable Class LibraryStartNew(Func(Of Object, TResult), Object)Creates and starts a task.
Public methodSupported by Portable Class LibraryStartNew(Func(Of Object, TResult), Object, CancellationToken)Creates and starts a task.
Public methodSupported by Portable Class LibraryStartNew(Func(Of Object, TResult), Object, TaskCreationOptions)Creates and starts a task.
Public methodSupported by Portable Class LibraryStartNew(Func(Of TResult), CancellationToken, TaskCreationOptions, TaskScheduler)Creates and starts a task.
Public methodSupported by Portable Class LibraryStartNew(Func(Of Object, TResult), Object, CancellationToken, TaskCreationOptions, TaskScheduler)Creates and starts a task.
Public methodSupported by Portable Class LibraryToStringReturns a string that represents the current object. (Inherited from Object.)
Top

The .NET Framework provides two factories for creating and scheduling tasks:

The TaskFactory(Of TResult) class allows you to do the following:

  • Create a task and start it immediately by calling the StartNew method. You can call the overloads of this method to create and execute a task that requires non-default arguments.

    Caution noteCaution

    Starting with the .NET Framework 4.5, the Task.Run method provides the easiest way to create a task with default configuration values and start it immediately.

  • Create a task that starts when any one of the tasks in an array has completed by calling the ContinueWhenAny or ContinueWhenAny method.

  • Create a task that starts when all the tasks in an array have completed by calling the ContinueWhenAll or ContinueWhenAll method.

The static Task(Of TResult).Factory property returns a default TaskFactory(Of TResult) object. You can also call one of the TaskFactory(Of TResult) class constructors to configure the Task(Of TResult) objects that the TaskFactory(Of TResult) class creates. The following example configures a new TaskFactory(Of TResult) object to create tasks that have a specified cancellation token, task creation options, continuation options, and a customized task scheduler.

Imports System.Collections.Generic
Imports System.Threading
Imports System.Threading.Tasks

Module Example
   Public Sub Main()
      Dim cts As New CancellationTokenSource()
      Dim factory As New TaskFactory(Of Integer)(
                         cts.Token,
                         TaskCreationOptions.PreferFairness,
                         TaskContinuationOptions.ExecuteSynchronously,
                         New CustomScheduler())

      Dim t2 = factory.StartNew(Function() DoWork())
      cts.Dispose()
   End Sub 

   Function DoWork() As Integer 
      Return If(Date.Now.Hour <= 12, 1, 2)
   End Function 
End Module

In most cases, you do not have to instantiate a new TaskFactory(Of TResult) instance. Instead, you can use the static Task(Of TResult).Factory property, which returns a factory object that uses default values. You can then call its methods to start new tasks or define task continuations. For an illustration, see the example.

NoteNote

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: Synchronization | ExternalThreading. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

The following example uses the static Factory property to make two calls to the TaskFactory(Of TResult).StartNew method. The first task returns a string array that is populated with the names of files in the user's MyDocuments directory, while the second returns a string array that is populated with the names of subdirectories of the user's MyDocuments directory. It then calls the TaskFactory.ContinueWhenAll(Task(), Action(Of Task())) method, which displays information about the number of files and directories in the arrays returned by the two tasks after they have completed execution.

Imports System.IO
Imports System.Threading.Tasks

Module Example
   Public Sub Main()
      Dim tasks(1) As Task(Of String())
      Dim docsDirectory As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

      tasks(0) = Task(Of String()).Factory.StartNew( Function()Directory.GetFiles(docsDirectory) )
''                                        End Sub )
      tasks(1) = Task(Of String()).Factory.StartNew( Function() Directory.GetDirectories(docsDirectory) )
''                                        End Sub )
      Task.Factory.ContinueWhenAll(tasks, Sub(completedTasks)
                                             Console.WriteLine("{0} contains: ", docsDirectory)
                                             Console.WriteLine("   {0} subdirectories", tasks(1).Result.Length)
                                             Console.WriteLine("   {0} files", tasks(0).Result.Length)
                                          End Sub)
   End Sub 
End Module 
' The example displays output like the following: 
'       C:\Users\<username>\Documents contains: 
'          24 subdirectories 
'          16 files

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

All public and protected members of TaskFactory(Of TResult) are thread-safe and may be used concurrently from multiple threads.

Show:
© 2015 Microsoft