Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Factory Property

Task.Factory Property

Provides access to factory methods for creating and configuring Task and Task(Of TResult) instances.

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

Public Shared ReadOnly Property Factory As TaskFactory

Property Value

Type: System.Threading.Tasks.TaskFactory
A factory object that can create a variety of Task and Task(Of TResult) objects.

This property returns a default instance of the TaskFactory class that is identical to the one created by calling the parameterless TaskFactory.TaskFactory constructor. It has the following property values:

The most common use of this property is to create and start a new task in a single call to the TaskFactory.StartNew method.


Starting with the .NET Framework 4.5, the Task.Run method provides the easiest way to create a Task object with default configuration values.

The following example uses the static Factory property to make two calls to the TaskFactory.StartNew method. The first populates an array with the names of files in the user's MyDocuments directory, while the second populates an array 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 two arrays after the first two tasks have completed execution.

Imports System.IO
Imports System.Threading.Tasks

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

      tasks(0) = Task.Factory.StartNew( Sub()
                                           files = Directory.GetFiles(docsDirectory)
                                        End Sub )
      tasks(1) = Task.Factory.StartNew( Sub()
                                           dirs = Directory.GetDirectories(docsDirectory)
                                        End Sub )
      Task.Factory.ContinueWhenAll(tasks, Sub(completedTasks)
                                             Console.WriteLine("{0} contains: ", docsDirectory)
                                             Console.WriteLine("   {0} subdirectories", dirs.Length)
                                             Console.WriteLine("   {0} files", files.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

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library
© 2015 Microsoft