Task<TResult> Constructors

Definition

Initializes a new Task<TResult> object.

Overloads

Task<TResult>(Func<TResult>)

Initializes a new Task<TResult> with the specified function.

Task<TResult>(Func<Object,TResult>, Object)

Initializes a new Task<TResult> with the specified function and state.

Task<TResult>(Func<TResult>, CancellationToken)

Initializes a new Task<TResult> with the specified function.

Task<TResult>(Func<TResult>, TaskCreationOptions)

Initializes a new Task<TResult> with the specified function and creation options.

Task<TResult>(Func<Object,TResult>, Object, CancellationToken)

Initializes a new Task<TResult> with the specified action, state, and options.

Task<TResult>(Func<Object,TResult>, Object, TaskCreationOptions)

Initializes a new Task<TResult> with the specified action, state, and options.

Task<TResult>(Func<TResult>, CancellationToken, TaskCreationOptions)

Initializes a new Task<TResult> with the specified function and creation options.

Task<TResult>(Func<Object,TResult>, Object, CancellationToken, TaskCreationOptions)

Initializes a new Task<TResult> with the specified action, state, and options.

Task<TResult>(Func<TResult>)

Source:
Future.cs
Source:
Future.cs
Source:
Future.cs

Initializes a new Task<TResult> with the specified function.

C#
public Task(Func<TResult> function);

Parameters

function
Func<TResult>

The delegate that represents the code to execute in the task. When the function has completed, the task's Result property will be set to return the result value of the function.

Exceptions

The function argument is null.

The function argument is null.

Examples

The following example counts the approximate number of words in text files that represent published books. Each task is responsible for opening a file, reading its entire contents asynchronously, and calculating the word count by using a regular expression. The Task.WaitAll(Task[]) method is called to ensure that all tasks have completed before displaying the word count of each book to the console.

Object instantiation is separated from object execution in this example so that the example can ensure that each file exists. If they do not, it displays the name of the missing file. Otherwise, it calls the Task.Start method to launch each task.

C#
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

public class Example
{
   public static async Task Main()
   {
      string pattern = @"\p{P}*\s+";
      string[] titles = { "Sister Carrie", "The Financier" };
      Task<int>[] tasks = new Task<int>[titles.Length];

      for (int ctr = 0; ctr < titles.Length; ctr++) {
         string s = titles[ctr];
         tasks[ctr] = new Task<int>( () => {
                                   // Number of words.
                                   int nWords = 0;
                                   // Create filename from title.
                                   string fn = s + ".txt";

                                   StreamReader sr = new StreamReader(fn);
                                   string input = sr.ReadToEndAsync().Result;
                                   sr.Close();
                                   nWords = Regex.Matches(input, pattern).Count;
                                   return nWords;
                                } );
      }
      // Ensure files exist before launching tasks.
      bool allExist = true;
      foreach (var title in titles) {
         string fn = title + ".txt";
         if (!File.Exists(fn)) {
            allExist = false;
            Console.WriteLine("Cannot find '{0}'", fn);
            break;
         }   
      }
      // Launch tasks 
      if (allExist) {
         foreach (var t in tasks)
            t.Start();
      
        await Task.WhenAll(tasks);
  
        Console.WriteLine("Word Counts:\n");
        for (int ctr = 0; ctr < titles.Length; ctr++)
           Console.WriteLine("{0}: {1,10:N0} words", titles[ctr], tasks[ctr].Result);
      }   
   }
}
// The example displays the following output:
//       Sister Carrie:    159,374 words
//       The Financier:    196,362 words

The regular expression pattern \p{P}*\s+ matches zero, one, or more punctuation characters followed by one or more white-space characters. It assumes that the total number of matches equals the approximate word count.

Remarks

This constructor should only be used in advanced scenarios where it is required that the creation and starting of the task is separated.

Rather than calling this constructor, the most common way to instantiate a Task<TResult> object and launch a task is by calling the static Task.Run<TResult>(Func<TResult>) and TaskFactory<TResult>.StartNew(Func<TResult>) methods.

If a task with no action is needed just for the consumer of an API to have something to await, a TaskCompletionSource<TResult> should be used.

See also

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Task<TResult>(Func<Object,TResult>, Object)

Source:
Future.cs
Source:
Future.cs
Source:
Future.cs

Initializes a new Task<TResult> with the specified function and state.

C#
public Task(Func<object,TResult> function, object state);
C#
public Task(Func<object?,TResult> function, object? state);

Parameters

function
Func<Object,TResult>

The delegate that represents the code to execute in the task. When the function has completed, the task's Result property will be set to return the result value of the function.

state
Object

An object representing data to be used by the action.

Exceptions

The function argument is null.

The function argument is null.

Remarks

Rather than calling this constructor, the most common way to instantiate a Task<TResult> object and launch a task is by calling the static TaskFactory<TResult>.StartNew(Func<Object,TResult>, Object) method. The only advantage offered by this constructor is that it allows object instantiation to be separated from task invocation.

See also

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Task<TResult>(Func<TResult>, CancellationToken)

Source:
Future.cs
Source:
Future.cs
Source:
Future.cs

Initializes a new Task<TResult> with the specified function.

C#
public Task(Func<TResult> function, System.Threading.CancellationToken cancellationToken);

Parameters

function
Func<TResult>

The delegate that represents the code to execute in the task. When the function has completed, the task's Result property will be set to return the result value of the function.

cancellationToken
CancellationToken

The CancellationToken to be assigned to this task.

Exceptions

The CancellationTokenSource that created cancellationToken has already been disposed.

The function argument is null.

The function argument is null.

Remarks

Rather than calling this constructor, the most common way to instantiate a Task<TResult> object and launch a task is by calling the static Task.Run<TResult>(Func<TResult>, CancellationToken) and TaskFactory<TResult>.StartNew(Func<TResult>, CancellationToken) methods. The only advantage offered by this constructor is that it allows object instantiation to be separated from task invocation.

See also

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Task<TResult>(Func<TResult>, TaskCreationOptions)

Source:
Future.cs
Source:
Future.cs
Source:
Future.cs

Initializes a new Task<TResult> with the specified function and creation options.

C#
public Task(Func<TResult> function, System.Threading.Tasks.TaskCreationOptions creationOptions);

Parameters

function
Func<TResult>

The delegate that represents the code to execute in the task. When the function has completed, the task's Result property will be set to return the result value of the function.

creationOptions
TaskCreationOptions

The TaskCreationOptions used to customize the task's behavior.

Exceptions

The creationOptions argument specifies an invalid value for TaskCreationOptions.

The function argument is null.

The function argument is null.

Remarks

Rather than calling this constructor, the most common way to instantiate a Task<TResult> object and launch a task is by calling the static TaskFactory<TResult>.StartNew(Func<TResult>, TaskCreationOptions) method. The only advantage offered by this constructor is that it allows object instantiation to be separated from task invocation.

See also

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Task<TResult>(Func<Object,TResult>, Object, CancellationToken)

Source:
Future.cs
Source:
Future.cs
Source:
Future.cs

Initializes a new Task<TResult> with the specified action, state, and options.

C#
public Task(Func<object,TResult> function, object state, System.Threading.CancellationToken cancellationToken);
C#
public Task(Func<object?,TResult> function, object? state, System.Threading.CancellationToken cancellationToken);

Parameters

function
Func<Object,TResult>

The delegate that represents the code to execute in the task. When the function has completed, the task's Result property will be set to return the result value of the function.

state
Object

An object representing data to be used by the function.

cancellationToken
CancellationToken

The CancellationToken to be assigned to the new task.

Exceptions

The CancellationTokenSource that created cancellationToken has already been disposed.

The function argument is null.

The function argument is null.

Remarks

Rather than calling this constructor, the most common way to instantiate a Task<TResult> object and launch a task is by calling the static TaskFactory<TResult>.StartNew(Func<Object,TResult>, Object, CancellationToken) method. The only advantage offered by this constructor is that it allows object instantiation to be separated from task invocation.

See also

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Task<TResult>(Func<Object,TResult>, Object, TaskCreationOptions)

Source:
Future.cs
Source:
Future.cs
Source:
Future.cs

Initializes a new Task<TResult> with the specified action, state, and options.

C#
public Task(Func<object,TResult> function, object state, System.Threading.Tasks.TaskCreationOptions creationOptions);
C#
public Task(Func<object?,TResult> function, object? state, System.Threading.Tasks.TaskCreationOptions creationOptions);

Parameters

function
Func<Object,TResult>

The delegate that represents the code to execute in the task. When the function has completed, the task's Result property will be set to return the result value of the function.

state
Object

An object representing data to be used by the function.

creationOptions
TaskCreationOptions

The TaskCreationOptions used to customize the task's behavior.

Exceptions

The creationOptions argument specifies an invalid value for TaskCreationOptions.

The function argument is null.

The function argument is null.

Remarks

Rather than calling this constructor, the most common way to instantiate a Task<TResult> object and launch a task is by calling the static TaskFactory<TResult>.StartNew(Func<Object,TResult>, Object, TaskCreationOptions) method. The only advantage offered by this constructor is that it allows object instantiation to be separated from task invocation.

See also

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Task<TResult>(Func<TResult>, CancellationToken, TaskCreationOptions)

Source:
Future.cs
Source:
Future.cs
Source:
Future.cs

Initializes a new Task<TResult> with the specified function and creation options.

C#
public Task(Func<TResult> function, System.Threading.CancellationToken cancellationToken, System.Threading.Tasks.TaskCreationOptions creationOptions);

Parameters

function
Func<TResult>

The delegate that represents the code to execute in the task. When the function has completed, the task's Result property will be set to return the result value of the function.

cancellationToken
CancellationToken

The CancellationToken that will be assigned to the new task.

creationOptions
TaskCreationOptions

The TaskCreationOptions used to customize the task's behavior.

Exceptions

The CancellationTokenSource that created cancellationToken has already been disposed.

The creationOptions argument specifies an invalid value for TaskCreationOptions.

The function argument is null.

The function argument is null.

Remarks

Rather than calling this constructor, the most common way to instantiate a Task<TResult> object and launch a task is by calling the static TaskFactory.StartNew<TResult>(Func<TResult>, CancellationToken, TaskCreationOptions, TaskScheduler) method. The only advantage offered by this constructor is that it allows object instantiation to be separated from task invocation.

See also

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Task<TResult>(Func<Object,TResult>, Object, CancellationToken, TaskCreationOptions)

Source:
Future.cs
Source:
Future.cs
Source:
Future.cs

Initializes a new Task<TResult> with the specified action, state, and options.

C#
public Task(Func<object,TResult> function, object state, System.Threading.CancellationToken cancellationToken, System.Threading.Tasks.TaskCreationOptions creationOptions);
C#
public Task(Func<object?,TResult> function, object? state, System.Threading.CancellationToken cancellationToken, System.Threading.Tasks.TaskCreationOptions creationOptions);

Parameters

function
Func<Object,TResult>

The delegate that represents the code to execute in the task. When the function has completed, the task's Result property will be set to return the result value of the function.

state
Object

An object representing data to be used by the function.

cancellationToken
CancellationToken

The CancellationToken to be assigned to the new task.

creationOptions
TaskCreationOptions

The TaskCreationOptions used to customize the task's behavior.

Exceptions

The CancellationTokenSource that created cancellationToken has already been disposed.

The creationOptions argument specifies an invalid value for TaskCreationOptions.

The function argument is null.

The function argument is null.

Remarks

Rather than calling this constructor, the most common way to instantiate a Task<TResult> object and launch a task is by calling the static TaskFactory<TResult>.StartNew(Func<Object,TResult>, Object, CancellationToken, TaskCreationOptions, TaskScheduler) method. The only advantage offered by this constructor is that it allows object instantiation to be separated from task invocation.

See also

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0