有关处理任务操作引发的异常的信息,请参阅
异常处理
。
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
可用于取消工作(如果尚未启动)的取消标记。
Run(Func<Task>, CancellationToken)
不会将
cancellationToken
传递到
action
。
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)
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)
TResult
以下示例计算文本文件中表示已出版书籍的大致字数。 每个任务负责打开文件、异步读取其整个内容以及使用正则表达式计算字数计数。
WaitAll(Task[])
调用 方法可确保在向控制台显示每本书的字数之前已完成所有任务。
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
正则表达式
\p{P}*\s+
匹配零个、一个或多个标点字符,后跟一个或多个空格字符。 它假定匹配总数等于近似字数。
方法
Run
是 方法的一种更简单的
TaskFactory.StartNew(Action)
替代方法。 它创建具有以下默认值的任务: