For information on handling exceptions thrown by task operations, see
Exception Handling
.
public:
static System::Threading::Tasks::Task ^ Run(Func<System::Threading::Tasks::Task ^> ^ function, System::Threading::CancellationToken cancellationToken);
public static System.Threading.Tasks.Task Run (Func<System.Threading.Tasks.Task> function, System.Threading.CancellationToken cancellationToken);
public static System.Threading.Tasks.Task Run (Func<System.Threading.Tasks.Task?> function, System.Threading.CancellationToken cancellationToken);
static member Run : Func<System.Threading.Tasks.Task> * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Shared Function Run (function As Func(Of Task), cancellationToken As CancellationToken) As Task
Parameters
A cancellation token that can be used to cancel the work if it has not yet started.
Run(Func<Task>, CancellationToken)
does not pass
cancellationToken
to
action
.
Returns
public:
generic <typename TResult>
static System::Threading::Tasks::Task<TResult> ^ Run(Func<System::Threading::Tasks::Task<TResult> ^> ^ function);
public static System.Threading.Tasks.Task<TResult> Run<TResult> (Func<System.Threading.Tasks.Task<TResult>> function);
public static System.Threading.Tasks.Task<TResult> Run<TResult> (Func<System.Threading.Tasks.Task<TResult>?> function);
static member Run : Func<System.Threading.Tasks.Task<'Result>> -> System.Threading.Tasks.Task<'Result>
Public Shared Function Run(Of TResult) (function As Func(Of Task(Of TResult))) As Task(Of TResult)
Type Parameters
TResult
public:
generic <typename TResult>
static System::Threading::Tasks::Task<TResult> ^ Run(Func<TResult> ^ function);
public static System.Threading.Tasks.Task<TResult> Run<TResult> (Func<TResult> function);
static member Run : Func<'Result> -> System.Threading.Tasks.Task<'Result>
Public Shared Function Run(Of TResult) (function As Func(Of TResult)) As Task(Of TResult)
Type Parameters
TResult
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
WaitAll(Task[])
method is called to ensure that all tasks have completed before displaying the word count of each book to the console.
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
public class Example
public static void 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] = Task.Run( () => {
// Number of words.
int nWords = 0;
// Create filename from title.
string fn = s + ".txt";
if (File.Exists(fn)) {
StreamReader sr = new StreamReader(fn);
string input = sr.ReadToEndAsync().Result;
nWords = Regex.Matches(input, pattern).Count;
return nWords;
Task.WaitAll(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
open System
open System.IO
open System.Text.RegularExpressions
open System.Threading.Tasks
let pattern = @"\p{P}*\s+"
let titles = [| "Sister Carrie"; "The Financier" |]
let tasks =
Array.map (fun title ->
Task.Run(fun () ->
// Create filename from title.
let fn = title + ".txt"
if File.Exists fn then
use sr = new StreamReader(fn)
let input = sr.ReadToEndAsync().Result
Regex.Matches(input, pattern).Count
0)) titles
tasks |> Seq.cast |> Array.ofSeq |> Task.WaitAll
printfn "Word Counts:\n"
for i = 0 to tasks.Length - 1 do
printfn $"%s{titles.[i]}: %10d{tasks.[i].Result} words"
// The example displays the following output:
// Sister Carrie: 159,374 words
// The Financier: 196,362 words
Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Threading.Tasks
Module Example
Public Sub Main()
Dim pattern As String = "\p{P}*\s+"
Dim titles() As String = { "Sister Carrie",
"The Financier" }
Dim tasks(titles.Length - 1) As Task(Of Integer)
For ctr As Integer = 0 To titles.Length - 1
Dim s As String = titles(ctr)
tasks(ctr) = Task.Run( Function()
' Number of words.
Dim nWords As Integer = 0
' Create filename from title.
Dim fn As String = s + ".txt"
If File.Exists(fn) Then
Dim sr As New StreamReader(fn)
Dim input As String = sr.ReadToEndAsync().Result
nWords = Regex.Matches(input, pattern).Count
End If
Return nWords
End Function)
Task.WaitAll(tasks)
Console.WriteLine("Word Counts:")
Console.WriteLine()
For ctr As Integer = 0 To titles.Length - 1
Console.WriteLine("{0}: {1,10:N0} words", titles(ctr), tasks(ctr).Result)
End Sub
End Module
' The example displays the following output:
' Sister Carrie: 159,374 words
' The Financier: 196,362 words
The regular expression
\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
The
Run
method is a simpler alternative to the
TaskFactory.StartNew(Action)
method. It creates a task with the following default values: