基于任务的异步编程

任务并行库 (TPL) 以“任务”的概念为基础,后者表示异步操作。 在某些方面,任务类似于线程或 ThreadPool 工作项,但是抽象级别更高。 术语“任务并行”是指一个或多个独立的任务同时运行。 任务提供两个主要好处:
系统资源的使用效率更高,可伸缩性更好。
在后台,任务排队到已使用算法增强的 ThreadPool,这些算法能够确定线程数并随之调整,提供负载平衡以实现吞吐量最大化。 这会使任务相对轻量,你可以创建很多任务以启用细化并行。
对于线程或工作项,可以使用更多的编程控件。
任务和围绕它们生成的框架提供了一组丰富的 API,这些 API 支持等待、取消、继续、可靠的异常处理、详细状态、自定义计划等功能。
出于这两个原因,在 .NET Framework 中,TPL 是用于编写多线程、异步和并行代码的首选 API。
隐式创建和运行任务
Parallel.Invoke 方法提供了一种简便方式,可同时运行任意数量的任意语句。 只需为每个工作项传入 Action 委托即可。 创建这些委托的最简单方式是使用 lambda 表达式。 lambda 表达式可调用指定的方法,或提供内联代码。 下面的示例演示一个基本的 Invoke 调用,该调用创建并启动同时运行的两个任务。 第一个任务由调用名为 DoSomeWork 的方法的 lambda 表达式表示,第二个任务由调用名为 DoSomeOtherWork 的方法的 lambda 表达式表示。
备注

本文档使用 lambda 表达式在 TPL 中定义委托。 如果不熟悉 C# 或 Visual Basic 中的 lambda 表达式,请参阅 PLINQ 和 TPL 中的 Lambda 表达式。
C#

复制
Parallel.Invoke(() => DoSomeWork(), () => DoSomeOtherWork());
备注

Task 在后台创建的 Invoke 实例数不一定与所提供的委托数相等。 TPL 可能会使用各种优化,特别是对于大量的委托。
有关详细信息,请参阅如何:使用 Parallel.Invoke 执行并行操作。
为了更好地控制任务执行或从任务返回值,必须更加显式地使用 Task 对象。
显式创建和运行任务
不返回值的任务由 System.Threading.Tasks.Task 类表示。 返回值的任务由 System.Threading.Tasks.Task 类表示,该类从 Task 继承。 任务对象处理基础结构详细信息,并提供可在任务的整个生存期内从调用线程访问的方法和属性。 例如,可以随时访问任务的 Status 属性,以确定它是已开始运行、已完成运行、已取消还是引发了异常。 状态由 TaskStatus 枚举表示。
在创建任务时,你赋予它一个用户委托,该委托封装该任务将执行的代码。 该委托可以表示为命名的委托、匿名方法或 lambda 表达式。 lambda 表达式可以包含对命名方法的调用,如下面的示例所示。 请注意,该示例包含对 Task.Wait 方法的调用,以确保任务在控制台模式应用程序结束之前完成执行。
C#

复制
using System;
using System.Threading;
using System.Threading.Tasks;

public class Example
{
public static void Main()
{
Thread.CurrentThread.Name = “Main”;

  // Create a task and supply a user delegate by using a lambda expression. 
  Task taskA = new Task( () => Console.WriteLine("Hello from taskA."));
  // Start the task.
  taskA.Start();

  // Output a message from the calling thread.
  Console.WriteLine("Hello from thread '{0}'.", 
                    Thread.CurrentThread.Name);
  taskA.Wait();

}
}
// The example displays output like the following:
// Hello from thread ‘Main’.
// Hello from taskA.
你还可以使用 Task.Run 方法通过一个操作创建并启动任务。 无论是哪个任务计划程序与当前线程关联,Run 方法都将使用默认的任务计划程序来管理任务。 不需要对任务的创建和计划进行更多控制时,首选 Run 方法创建并启动任务。
C#

复制
using System;
using System.Threading;
using System.Threading.Tasks;

public class Example
{
public static void Main()
{
Thread.CurrentThread.Name = “Main”;

  // Define and run the task.
  Task taskA = Task.Run( () => Console.WriteLine("Hello from taskA."));

  // Output a message from the calling thread.
  Console.WriteLine("Hello from thread '{0}'.", 
                      Thread.CurrentThread.Name);
  taskA.Wait();

}
}
// The example displays output like the following:
// Hello from thread ‘Main’.
// Hello from taskA.
你还可以使用 TaskFactory.StartNew 方法在一个操作中创建并启动任务。 不必将创建和计划分开并且需要其他任务创建选项或使用特定计划程序时,或者需要将其他状态传递到可以通过 Task.AsyncState 属性检索到的任务时,请使用此方法,如下例所示。
C#

复制
using System;
using System.Threading;
using System.Threading.Tasks;

class CustomData
{
public long CreationTime;
public int Name;
public int ThreadNum;
}

public class Example
{
public static void Main()
{
Task[] taskArray = new Task[10];
for (int i = 0; i < taskArray.Length; i++) {
taskArray[i] = Task.Factory.StartNew( (Object obj ) => {
CustomData data = obj as CustomData;
if (data == null)
return;

                                              data.ThreadNum = Thread.CurrentThread.ManagedThreadId;
                                           },
                                           new CustomData() {Name = i, CreationTime = DateTime.Now.Ticks} );
  }
  Task.WaitAll(taskArray);     
  foreach (var task in taskArray) {
     var data = task.AsyncState as CustomData;
     if (data != null)
        Console.WriteLine("Task #{0} created at {1}, ran on thread #{2}.",
                          data.Name, data.CreationTime, data.ThreadNum);
  }                     

}
}
// The example displays output like the following:
// Task #0 created at 635116412924597583 on thread #3.
// Task #1 created at 635116412924607584 on thread #4.
// Task #3 created at 635116412924607584 on thread #4.
// Task #4 created at 635116412924607584 on thread #4.
// Task #2 created at 635116412924607584 on thread #3.
// Task #6 created at 635116412924607584 on thread #3.
// Task #5 created at 635116412924607584 on thread #4.
// Task #8 created at 635116412924607584 on thread #4.
// Task #7 created at 635116412924607584 on thread #3.
// Task #9 created at 635116412924607584 on thread #4.
Task 和 Task 均公开静态 Factory 属性,该属性返回 TaskFactory 的默认实例,因此你可以调用该方法为 Task.Factory.StartNew()。 此外,在以下示例中,由于任务的类型为 System.Threading.Tasks.Task,因此每个任务都具有包含计算结果的公共 Task.Result 属性。 任务以异步方式运行,可以按任意顺序完成。 如果在计算完成之前访问 Result 属性,则该属性将阻止调用线程,直到值可用为止。
C#

复制
using System;
using System.Threading.Tasks;

public class Example
{
public static void Main()
{
Task[] taskArray = { Task.Factory.StartNew(() => DoComputation(1.0)),
Task.Factory.StartNew(() => DoComputation(100.0)),
Task.Factory.StartNew(() => DoComputation(1000.0)) };

    var results = new Double[taskArray.Length];
    Double sum = 0;
    
    for (int i = 0; i < taskArray.Length; i++) {
        results[i] = taskArray[i].Result;
        Console.Write("{0:N1} {1}", results[i], 
                          i == taskArray.Length - 1 ? "= " : "+ ");
        sum += results[i];
    }
    Console.WriteLine("{0:N1}", sum);

}

private static Double DoComputation(Double start)
{
Double sum = 0;
for (var value = start; value <= start + 10; value += .1)
sum += value;

  return sum; 

}
}
// The example displays the following output:
// 606.0 + 10,605.0 + 100,495.0 = 111,706.0
有关详细信息,请参阅如何:从任务返回值。
使用 lambda 表达式创建委托时,你有权访问源代码中当时可见的所有变量。 然而,在某些情况下,特别是在循环中,lambda 不按照预期的方式捕获变量。 它仅捕获最终值,而不是它每次迭代后更改的值。 以下示例演示了该问题。 它将循环计数器传递给实例化 CustomData 对象并使用循环计数器作为对象标识符的 lambda 表达式。 如示例输出所示,每个 CustomData 对象都具有相同的标识符。
C#

复制
using System;
using System.Threading;
using System.Threading.Tasks;

class CustomData
{
public long CreationTime;
public int Name;
public int ThreadNum;
}

public class Example
{
public static void Main()
{
// Create the task object by using an Action(Of Object) to pass in the loop
// counter. This produces an unexpected result.
Task[] taskArray = new Task[10];
for (int i = 0; i < taskArray.Length; i++) {
taskArray[i] = Task.Factory.StartNew( (Object obj) => {
var data = new CustomData() {Name = i, CreationTime = DateTime.Now.Ticks};
data.ThreadNum = Thread.CurrentThread.ManagedThreadId;
Console.WriteLine(“Task #{0} created at {1} on thread #{2}.”,
data.Name, data.CreationTime, data.ThreadNum);
},
i );
}
Task.WaitAll(taskArray);
}
}
// The example displays output like the following:
// Task #10 created at 635116418427727841 on thread #4.
// Task #10 created at 635116418427737842 on thread #4.
// Task #10 created at 635116418427737842 on thread #4.
// Task #10 created at 635116418427737842 on thread #4.
// Task #10 created at 635116418427737842 on thread #4.
// Task #10 created at 635116418427737842 on thread #4.
// Task #10 created at 635116418427727841 on thread #3.
// Task #10 created at 635116418427747843 on thread #3.
// Task #10 created at 635116418427747843 on thread #3.
// Task #10 created at 635116418427737842 on thread #4.
通过使用构造函数向任务提供状态对象,可以在每次迭代时访问该值。 以下示例在上一示例的基础上做了修改,在创建 CustomData 对象时使用循环计数器,该对象继而传递给 lambda 表达式。 如示例输出所示,每个 CustomData 对象现在都具有唯一的一个标识符,该标识符基于该对象实例化时循环计数器的值。
C#

复制
using System;
using System.Threading;
using System.Threading.Tasks;

class CustomData
{
public long CreationTime;
public int Name;
public int ThreadNum;
}

public class Example
{
public static void Main()
{
// Create the task object by using an Action(Of Object) to pass in custom data
// to the Task constructor. This is useful when you need to capture outer variables
// from within a loop.
Task[] taskArray = new Task[10];
for (int i = 0; i < taskArray.Length; i++) {
taskArray[i] = Task.Factory.StartNew( (Object obj ) => {
CustomData data = obj as CustomData;
if (data == null)
return;

                                              data.ThreadNum = Thread.CurrentThread.ManagedThreadId;
                                              Console.WriteLine("Task #{0} created at {1} on thread #{2}.",
                                                               data.Name, data.CreationTime, data.ThreadNum);
                                           },
                                           new CustomData() {Name = i, CreationTime = DateTime.Now.Ticks} );
  }
  Task.WaitAll(taskArray);     

}
}
// The example displays output like the following:
// Task #0 created at 635116412924597583 on thread #3.
// Task #1 created at 635116412924607584 on thread #4.
// Task #3 created at 635116412924607584 on thread #4.
// Task #4 created at 635116412924607584 on thread #4.
// Task #2 created at 635116412924607584 on thread #3.
// Task #6 created at 635116412924607584 on thread #3.
// Task #5 created at 635116412924607584 on thread #4.
// Task #8 created at 635116412924607584 on thread #4.
// Task #7 created at 635116412924607584 on thread #3.
// Task #9 created at 635116412924607584 on thread #4.
此状态作为参数传递给任务委托,并且可通过使用 Task.AsyncState 属性从任务对象访问。 以下示例在上一示例的基础上演变而来。 它使用 AsyncState 属性显示关于传递到 lambda 表达式的 CustomData 对象的信息。
C#

复制
using System;
using System.Threading;
using System.Threading.Tasks;

class CustomData
{
public long CreationTime;
public int Name;
public int ThreadNum;
}

public class Example
{
public static void Main()
{
Task[] taskArray = new Task[10];
for (int i = 0; i < taskArray.Length; i++) {
taskArray[i] = Task.Factory.StartNew( (Object obj ) => {
CustomData data = obj as CustomData;
if (data == null)
return;

                                              data.ThreadNum = Thread.CurrentThread.ManagedThreadId;
                                           },
                                           new CustomData() {Name = i, CreationTime = DateTime.Now.Ticks} );
  }
  Task.WaitAll(taskArray);     
  foreach (var task in taskArray) {
     var data = task.AsyncState as CustomData;
     if (data != null)
        Console.WriteLine("Task #{0} created at {1}, ran on thread #{2}.",
                          data.Name, data.CreationTime, data.ThreadNum);
  }                     

}
}
// The example displays output like the following:
// Task #0 created at 635116412924597583 on thread #3.
// Task #1 created at 635116412924607584 on thread #4.
// Task #3 created at 635116412924607584 on thread #4.
// Task #4 created at 635116412924607584 on thread #4.
// Task #2 created at 635116412924607584 on thread #3.
// Task #6 created at 635116412924607584 on thread #3.
// Task #5 created at 635116412924607584 on thread #4.
// Task #8 created at 635116412924607584 on thread #4.
// Task #7 created at 635116412924607584 on thread #3.
// Task #9 created at 635116412924607584 on thread #4.
任务 ID
每个任务都获得一个在应用程序域中唯一标识自己的整数 ID,可以使用 Task.Id 属性访问该 ID。 该 ID 可有效用于在 Visual Studio 调试器的“并行堆栈”和“任务”窗口中查看任务信息。 该 ID 是惰式创建的,这意味着它不会在被请求之前创建;因此每次运行该程序时,任务可能具有不同的 ID。 有关如何在调试器中查看任务 ID 的详细信息,请参阅使用任务窗口和使用并行堆栈窗口。
任务创建选项
创建任务的大多数 API 提供接受 TaskCreationOptions 参数的重载。 通过指定下列选项之一,可指示任务计划程序如何在线程池中安排任务计划。 下表列出了各种任务创建选项。
TaskCreationOptions 参数值 描述
None 未指定任何选项时的默认值。 计划程序将使用其默认试探法来计划任务。
PreferFairness 指定应当计划任务,以使越早创建的任务将更可能越早执行,而越晚创建的任务将更可能越晚执行。
LongRunning 指定该任务表示长时间运行的运算。
AttachedToParent 指定应将任务创建为当前任务(如果存在)的附加子级。 有关详细信息,请参阅附加和分离的子任务。
DenyChildAttach 指定如果内部任务指定 AttachedToParent 选项,则该任务不会成为附加的子任务。
HideScheduler 指定通过调用特定任务内部的 TaskFactory.StartNew 或 Task.ContinueWith 等方法创建的任务的任务计划程序是默认计划程序,而不是正在运行此任务的计划程序。
可以通过使用位 OR 运算组合选项。 下面的示例演示一个具有 LongRunning 和 PreferFairness 选项的任务。
C#

复制
var task3 = new Task(() => MyLongRunningMethod(),
TaskCreationOptions.LongRunning | TaskCreationOptions.PreferFairness);
task3.Start();
任务、线程和区域性
每个线程都具有一个关联的区域性和 UI 区域性,分别由 Thread.CurrentCulture 和 Thread.CurrentUICulture 属性定义。 线程的区域性用在诸如格式、分析、排序和字符串比较操作中。 线程的 UI 区域性用于查找资源。 通常,除非使用 CultureInfo.DefaultThreadCurrentCulture 和 CultureInfo.DefaultThreadCurrentUICulture 属性在应用程序域中为所有线程指定默认区域性,线程的默认区域性和 UI 区域性则由系统区域性定义。 如果你显式设置线程的区域性并启动新线程,则新线程不会继承正在调用的线程的区域性;相反,其区域性就是默认系统区域性。 基于任务的应用编程模型遵循这种做法,这些应用指定 .NET Framework 4.6 之前的 .NET Framework 版本。
重要

请注意,作为任务上下文一部分的调用线程的区域性适用于面向 .NET Framework 4.6 的应用,而不是在 .NET Framework 4.6 下运行的应用。 可以在 Visual Studio 中创建项目时,定目标到特定版本的 .NET Framework,具体操作是从“新建项目”对话框顶部的下拉列表选择相应版本。在 Visual Studio 外部,可以使用 TargetFrameworkAttribute 属性定目标到特定版本。 对于指定 .NET Framework 4.6 之前的 .NET Framework 版本的应用,或者对于不指定 .NET framework 特定版本的应用,任务的区域性将继续由它运行的线程的区域性确定。
启动指定 .NET Framework 4.6 的应用程序,即使任务在线程池线程上以异步方式运行,调用线程的区域性仍然通过每个任务继承。
下面的示例提供了简单的演示。 它使用 TargetFrameworkAttribute 特性来指定 .NET Framework 4.6,并将应用程序的当前区域性更改为 French (France),或者,如果 French (France) 已为当前区域性,则更改为 English (United States)。 然后,调用一个名为 formatDelegate 的委托,该委托返回在新区域性中格式化为货币值的数字。 注意无论该委托是同步还是异步作为任务,它都将返回预期的结果,因为调用线程的区域性是由异步任务继承的。
C#

复制
using System;
using System.Globalization;
using System.Runtime.Versioning;
using System.Threading;
using System.Threading.Tasks;

[assembly:TargetFramework(".NETFramework,Version=v4.6")]

public class Example
{

public static void Main()
{
decimal[] values = { 163025412.32m, 18905365.59m };
string formatString = “C2”;
Func formatDelegate = () => { string output = String.Format(“Formatting using the {0} culture on thread {1}.\n”,
CultureInfo.CurrentCulture.Name,
Thread.CurrentThread.ManagedThreadId);
foreach (var value in values)
output += String.Format("{0} ", value.ToString(formatString));

                                         output += Environment.NewLine;
                                         return output;
                                       };
   
   Console.WriteLine("The example is running on thread {0}", 
                     Thread.CurrentThread.ManagedThreadId);
   // Make the current culture different from the system culture.
   Console.WriteLine("The current culture is {0}", 
                     CultureInfo.CurrentCulture.Name);
   if (CultureInfo.CurrentCulture.Name == "fr-FR")
      Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
   else
      Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");

   Console.WriteLine("Changed the current culture to {0}.\n",
                     CultureInfo.CurrentCulture.Name);
   
   // Execute the delegate synchronously.
   Console.WriteLine("Executing the delegate synchronously:");
   Console.WriteLine(formatDelegate());
   
   // Call an async delegate to format the values using one format string.
   Console.WriteLine("Executing a task asynchronously:"); 
   var t1 = Task.Run(formatDelegate);
   Console.WriteLine(t1.Result);
   
   Console.WriteLine("Executing a task synchronously:");
   var t2 = new Task<String>(formatDelegate); 
   t2.RunSynchronously();
   Console.WriteLine(t2.Result);

}
}
// The example displays the following output:
// The example is running on thread 1
// The current culture is en-US
// Changed the current culture to fr-FR.
//
// Executing the delegate synchronously:
// Formatting using the fr-FR culture on thread 1.
// 163 025 412,32 € 18 905 365,59 €
//
// Executing a task asynchronously:
// Formatting using the fr-FR culture on thread 3.
// 163 025 412,32 € 18 905 365,59 €
//
// Executing a task synchronously:
// Formatting using the fr-FR culture on thread 1.
// 163 025 412,32 € 18 905 365,59 €
// If the TargetFrameworkAttribute statement is removed, the example
// displays the following output:
// The example is running on thread 1
// The current culture is en-US
// Changed the current culture to fr-FR.
//
// Executing the delegate synchronously:
// Formatting using the fr-FR culture on thread 1.
// 163 025 412,32 € 18 905 365,59 €
//
// Executing a task asynchronously:
// Formatting using the en-US culture on thread 3.
// $163,025,412.32 $18,905,365.59
//
// Executing a task synchronously:
// Formatting using the fr-FR culture on thread 1.
// 163 025 412,32 € 18 905 365,59 €
如果使用的是 Visual Studio,可以省略 TargetFrameworkAttribute 属性,并在创建项目时在“新建项目”对话框中改选“.NET Framework 4.6”作为目标。
对于反映指定 .NET Framework 4.6 之前的 .NET Framework 版本的应用程序行为的输出,请从源代码中移除 TargetFrameworkAttribute 属性。 输出将会反应默认系统区域性的格式设置约定,而不是调用线程的区域性。
有关异步任务和区域性的详细信息,请参阅 CultureInfo 主题中的“区域性和基于异步任务的操作”部分。
创建任务延续
使用 Task.ContinueWith 和 Task.ContinueWith 方法,可以指定要在先行任务完成时启动的任务。 延续任务的委托已传递了对先行任务的引用,因此它可以检查先行任务的状态,并通过检索 Task.Result 属性的值将先行任务的输出用作延续任务的输入。
在下面的示例中,getData 任务通过调用 TaskFactory.StartNew(Func) 方法来启动。 当 processData 完成时,getData 任务自动启动,当 displayData 完成时,processData 启动。 getData 产生一个整数数组,通过 processData 任务的 getData 属性,Task.Result 任务可访问该数组。 processData 任务处理该数组并返回结果,结果的类型从传递到 Task.ContinueWith(Func<Task,TNewResult>) 方法的 lambda 表达式的返回类型推断而来。 displayData 完成时,processData 任务自动执行,而 Tuple<T1,T2,T3> 任务可通过 processData 任务的 displayData 属性访问由 processData lambda 表达式返回的 Task.Result 对象。 displayData 任务采用 processData 任务的结果,继而得出自己的结果,其类型以相似方式推断而来,且可由程序中的 Result 属性使用。
C#

复制
using System;
using System.Threading.Tasks;

public class Example
{
public static void Main()
{
var getData = Task.Factory.StartNew(() => {
Random rnd = new Random();
int[] values = new int[100];
for (int ctr = 0; ctr <= values.GetUpperBound(0); ctr++)
values[ctr] = rnd.Next();

                                         return values;
                                      } );  
  var processData = getData.ContinueWith((x) => {
                                            int n = x.Result.Length;
                                            long sum = 0;
                                            double mean;
                              
                                            for (int ctr = 0; ctr <= x.Result.GetUpperBound(0); ctr++)
                                               sum += x.Result[ctr];

                                            mean = sum / (double) n;
                                            return Tuple.Create(n, sum, mean);
                                         } ); 
  var displayData = processData.ContinueWith((x) => {
                                                return String.Format("N={0:N0}, Total = {1:N0}, Mean = {2:N2}",
                                                                     x.Result.Item1, x.Result.Item2, 
                                                                     x.Result.Item3);
                                             } );                         
  Console.WriteLine(displayData.Result);

}
}
// The example displays output similar to the following:
// N=100, Total = 110,081,653,682, Mean = 1,100,816,536.82
因为 Task.ContinueWith 是实例方法,所以你可以将方法调用链接在一起,而不是为每个先行任务去实例化 Task 对象。 以下示例与上一示例在功能上等同,唯一的不同在于它将对 Task.ContinueWith 方法的调用链接在一起。 请注意,通过方法调用链返回的 Task 对象是最终延续任务。
C#

复制
using System;
using System.Threading.Tasks;

public class Example
{
public static void Main()
{
var displayData = Task.Factory.StartNew(() => {
Random rnd = new Random();
int[] values = new int[100];
for (int ctr = 0; ctr <= values.GetUpperBound(0); ctr++)
values[ctr] = rnd.Next();

                                             return values;
                                          } ).  
                    ContinueWith((x) => {
                                    int n = x.Result.Length;
                                    long sum = 0;
                                    double mean;
                              
                                    for (int ctr = 0; ctr <= x.Result.GetUpperBound(0); ctr++)
                                       sum += x.Result[ctr];

                                    mean = sum / (double) n;
                                    return Tuple.Create(n, sum, mean);
                                 } ). 
                    ContinueWith((x) => {
                                    return String.Format("N={0:N0}, Total = {1:N0}, Mean = {2:N2}",
                                                         x.Result.Item1, x.Result.Item2, 
                                                         x.Result.Item3);
                                 } );                         
  Console.WriteLine(displayData.Result);

}
}
// The example displays output similar to the following:
// N=100, Total = 110,081,653,682, Mean = 1,100,816,536.82
使用 ContinueWhenAll 和 ContinueWhenAny 方法,可以从多个任务继续。
有关详细信息,请参阅使用延续任务链接任务。
创建分离的子任务
如果在任务中运行的用户代码创建一个新任务,且未指定 AttachedToParent 选项,则该新任务不采用任何特殊方式与父任务同步。 这种不同步的任务类型称为“分离的嵌套任务”或“分离的子任务”。 以下示例展示了创建一个分离子任务的任务。
C#

复制
var outer = Task.Factory.StartNew(() =>
{
Console.WriteLine(“Outer task beginning.”);

var child = Task.Factory.StartNew(() =>
{
    Thread.SpinWait(5000000);
    Console.WriteLine("Detached task completed.");
});

});

outer.Wait();
Console.WriteLine(“Outer task completed.”);
// The example displays the following output:
// Outer task beginning.
// Outer task completed.
// Detached task completed.
请注意,父任务不会等待分离子任务完成。
创建子任务
如果任务中运行的用户代码在创建任务时指定了 AttachedToParent 选项,新任务就称为父任务的附加子任务。 因为父任务隐式地等待所有附加子任务完成,所以你可以使用 AttachedToParent 选项表示结构化的任务并行。 以下示例展示了创建十个附加子任务的父任务。 请注意,虽然此示例调用 Task.Wait 方法等待父任务完成,但不必显式等待附加子任务完成。
C#

复制
using System;
using System.Threading;
using System.Threading.Tasks;

public class Example
{
public static void Main()
{
var parent = Task.Factory.StartNew(() => {
Console.WriteLine(“Parent task beginning.”);
for (int ctr = 0; ctr < 10; ctr++) {
int taskNo = ctr;
Task.Factory.StartNew((x) => {
Thread.SpinWait(5000000);
Console.WriteLine(“Attached child #{0} completed.”,
x);
},
taskNo, TaskCreationOptions.AttachedToParent);
}
});

  parent.Wait();
  Console.WriteLine("Parent task completed.");

}
}
// The example displays output like the following:
// Parent task beginning.
// Attached child #9 completed.
// Attached child #0 completed.
// Attached child #8 completed.
// Attached child #1 completed.
// Attached child #7 completed.
// Attached child #2 completed.
// Attached child #6 completed.
// Attached child #3 completed.
// Attached child #5 completed.
// Attached child #4 completed.
// Parent task completed.
父任务可使用 TaskCreationOptions.DenyChildAttach 选项阻止其他任务附加到父任务。 有关详细信息,请参阅附加和分离的子任务。
等待任务完成
System.Threading.Tasks.Task 和 System.Threading.Tasks.Task 类型提供了 Task.Wait 和 System.Threading.Tasks.Task.Wait 方法的若干重载,以便能够等待任务完成。 此外,使用静态 Task.WaitAll 和 Task.WaitAny 方法的重载可以等待一批任务中的任一任务或所有任务完成。
通常,会出于以下某个原因等待任务:
主线程依赖于任务计算的最终结果。
你必须处理可能从任务引发的异常。
应用程序可以在所有任务执行完毕之前终止。 例如,执行 Main(应用程序入口点)中的所有同步代码后,控制台应用程序将立即终止。
下面的示例演示不包含异常处理的基本模式。
C#

复制
Task[] tasks = new Task[3]
{
Task.Factory.StartNew(() => MethodA()),
Task.Factory.StartNew(() => MethodB()),
Task.Factory.StartNew(() => MethodC())
};

//Block until all tasks complete.
Task.WaitAll(tasks);

// Continue on this thread…
有关演示异常处理的示例,请参见异常处理。
某些重载允许你指定超时,而其他重载采用额外的 CancellationToken 作为输入参数,以便可以通过编程方式或根据用户输入来取消等待。
等待任务时,其实是在隐式等待使用 TaskCreationOptions.AttachedToParent 选项创建的该任务的所有子级。 Task.Wait 在该任务已完成时立即返回。 Task.Wait 方法将抛出由某任务引发的任何异常,即使 Task.Wait 方法是在该任务完成之后调用的。
组合任务
Task 类和 Task 类提供多种方法,这些方法能够帮助你组合多个任务以实现常见模式,并更好地使用由 C#、Visual Basic 和 F# 提供的异步语言功能。 本节介绍了 WhenAll、WhenAny、Delay 和 FromResult 方法。
Task.WhenAll
Task.WhenAll 方法异步等待多个 Task 或 Task 对象完成。 通过它提供的重载版本可以等待非均匀任务组。 例如,你可以等待多个 Task 和 Task 对象在一个方法调用中完成。
Task.WhenAny
Task.WhenAny 方法异步等待多个 Task 或 Task 对象中的一个完成。 与在 Task.WhenAll 方法中一样,该方法提供重载版本,让你能等待非均匀任务组。 WhenAny 方法在下列情境中尤其有用。
冗余运算。 请考虑可以用多种方式执行的算法或运算。 你可使用 WhenAny 方法来选择先完成的运算,然后取消剩余的运算。
交叉运算。 你可启动必须全部完成的多项运算,并使用 WhenAny 方法在每项运算完成时处理结果。 在一项运算完成后,可以启动一个或多个其他任务。
受限制的运算。 你可使用 WhenAny 方法通过限制并发运算的数量来扩展前面的情境。
过期的运算。 你可使用 WhenAny 方法在一个或多个任务与特定时间后完成的任务(例如 Delay 方法返回的任务)间进行选择。 下节描述了 Delay 方法。
Task.Delay
Task.Delay 方法将生成在指定时间后完成的 Task 对象。 你可使用此方法来生成偶尔轮询数据的循环,引入超时,将对用户输入的处理延迟预定的一段时间等。
Task(T).FromResult
通过使用 Task.FromResult 方法,你可以创建包含预计算结果的 Task 对象。 执行返回 Task 对象的异步运算,且已计算该 Task 对象的结果时,此方法将十分有用。 有关使用 FromResult 检索缓存中包含的异步下载操作结果的示例,请参阅如何:创建预先计算的任务。
处理任务中的异常
当某个任务抛出一个或多个异常时,异常包装在 AggregateException 异常中。 该异常传播回与该任务联接的线程,通常该线程正在等待该任务完成或该线程访问 Result 属性。 此行为用于强制实施 .NET Framework 策略 - 默认所有未处理的异常应终止进程。 调用代码可以通过使用 try/catch 块中的以下任意方法来处理异常:
Wait 方法
WaitAll 方法
WaitAny 方法
Result 属性
联接线程也可以通过在对任务进行垃圾回收之前访问 Exception 属性来处理异常。 通过访问此属性,可防止未处理的异常在对象完成时触发终止进程的异常传播行为。
有关异常和任务的的详细信息,请参阅异常处理。
取消任务
Task 类支持协作取消,并与 .NET Framework 4 中新增的 System.Threading.CancellationTokenSource 类和 System.Threading.CancellationToken 类完全集成。 System.Threading.Tasks.Task 类中的大多数构造函数采用 CancellationToken 对象作为输入参数。 许多 StartNew 和 Run 重载还包括 CancellationToken 参数。
你可以创建标记,并使用 CancellationTokenSource 类在以后某一时间发出取消请求。 可以将该标记作为参数传递给 Task,还可以在执行响应取消请求的工作的用户委托中引用同一标记。
有关详细信息,请参阅任务取消和如何:取消任务及其子任务。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值