Task.Factory Task.Factory Task.Factory Task.Factory Property

Definizione

Fornisce l'accesso ai metodi factory per la creazione e la configurazione delle istanze di Task e Task<TResult>.Provides access to factory methods for creating and configuring Task and Task<TResult> instances.

public:
 static property System::Threading::Tasks::TaskFactory ^ Factory { System::Threading::Tasks::TaskFactory ^ get(); };
public static System.Threading.Tasks.TaskFactory Factory { get; }
member this.Factory : System.Threading.Tasks.TaskFactory
Public Shared ReadOnly Property Factory As TaskFactory

Valore della proprietà

Oggetto factory in grado di creare una vasta gamma di oggetti Task e Task<TResult>.A factory object that can create a variety of Task and Task<TResult> objects.

Commenti

Questa proprietà restituisce un'istanza predefinita di TaskFactory class identica a quella creata tramite la chiamata senza parametri TaskFactory.TaskFactory() costruttore.This property returns a default instance of the TaskFactory class that is identical to the one created by calling the parameterless TaskFactory.TaskFactory() constructor. Include i valori delle proprietà seguenti:It has the following property values:

ProprietàProperty ValueValue
TaskFactory.CancellationToken CancellationToken.None
TaskFactory.ContinuationOptions TaskContinuationOptions.None
TaskFactory.CreationOptions TaskCreationOptions.None
TaskFactory.Scheduler null, o TaskScheduler.Currentnull, or TaskScheduler.Current

L'uso più comune di questa proprietà consiste nel creare e avviare una nuova attività in una singola chiamata al TaskFactory.StartNew (metodo).The most common use of this property is to create and start a new task in a single call to the TaskFactory.StartNew method.

Nota

Inizia con la .NET Framework 4.5.NET Framework 4.5, il Task.Run metodo fornisce il modo più semplice per creare un Task oggetto con valori di configurazione predefiniti.Starting with the .NET Framework 4.5.NET Framework 4.5, the Task.Run method provides the easiest way to create a Task object with default configuration values.

L'esempio seguente usa il metodo statico Factory proprietà effettuare due chiamate al TaskFactory.StartNew (metodo).The following example uses the static Factory property to make two calls to the TaskFactory.StartNew method. Il primo popola una matrice con i nomi dei file nella directory di documenti dell'utente, mentre il secondo popola una matrice con i nomi delle sottodirectory della directory documenti dell'utente.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. Chiama quindi il TaskFactory.ContinueWhenAll(Task[], Action<Task[]>) (metodo), che visualizza informazioni sul numero di file e directory in due matrici dopo le prime due attività ha completato l'esecuzione.It then calls the TaskFactory.ContinueWhenAll(Task[], Action<Task[]>) method, which displays information about the number of files and directories in the two arrays after the first two tasks have completed execution.

using System;
using System.IO;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
      Task[] tasks = new Task[2];
      String[] files = null;
      String[] dirs = null;
      String docsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

      tasks[0] = Task.Factory.StartNew( () => files = Directory.GetFiles(docsDirectory));
      tasks[1] = Task.Factory.StartNew( () => dirs = Directory.GetDirectories(docsDirectory));

      Task.Factory.ContinueWhenAll(tasks, completedTasks => {
                                             Console.WriteLine("{0} contains: ", docsDirectory);
                                             Console.WriteLine("   {0} subdirectories", dirs.Length);
                                             Console.WriteLine("   {0} files", files.Length);
                                          } );
   }
}
// The example displays output like the following:
//       C:\Users\<username>\Documents contains:
//          24 subdirectories
//          16 files
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

Si applica a

Vedi anche