.NET framework 中支持的三个异步编程模式的简要概述:描述基于任务的编程模型 ,该模型简化了并行开发,使您能够通过固有方法编写高效、细化且可伸缩的并行代码,而不必直接处理线程或线程池。
異步編程模型APM ( Asynchronous Programming ) (旧版)
基於事件的異步編程設計模式(EAP) (旧版)
基於任務的異步模式(TAP)(建议用于新开发)
基于任务的异步模式 (TAP) 是基于 System.Threading.Tasks 命名空间的 Task 和 Task<TResult>,用于表示任意异步操作。
该 System.Threading.Tasks 命名空间提供简化编写并发和异步代码的工作的类型。 System.Threading.Tasks.Task which represents an asynchronous operation that can be waited on and cancelled, and System.Threading.Tasks.Task<TResult>, which is a task that can return a value." id="mt2">主要类型为 System.Threading.Tasks.Task(表示可以等待和取消的异步操作)和 System.Threading.Tasks.Task<TResult>(可以返回值的任务)。 System.Threading.Tasks.TaskFactory class provides static methods for creating and starting tasks, and the System.Threading.Tasks.TaskScheduler class provides the default thread scheduling infrastructure." id="mt3">System.Threading.Tasks.TaskFactory 类提供用于创建和启动任务的静态方法,System.Threading.Tasks.TaskScheduler 类提供默认线程调度基础结构。
Task 类还提供了初始化任务但不计划执行任务的构造函数。StartNew method should be the preferred mechanism for creating and scheduling computational tasks, but for scenarios where creation and scheduling must be separated, the constructors may be used, and the task's Start method may then be used to schedule the task for execution at a later time." id="mt103">出于性能方面的考虑,TaskFactory 的 StartNew 方法应该是创建和计划计算任务的首选机制,但是对于创建和计划必须分开的情况,可以使用构造函数,然后可以使用任务的 Start 方法计划任务在稍后执行。
using System;結果:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;//.Net Framework 4.0以上版本
namespace TestTask
{
class Program
{
// Demonstrated features:
// Task ctor()
// Task.Factory
// Task.Wait()
// Task.RunSynchronously()
// Expected results:
// Task t1 (alpha) is created unstarted.
// Task t2 (beta) is created started.
// Task t1's (alpha) start is held until after t2 (beta) is started.
// Both tasks t1 (alpha) and t2 (beta) are potentially executed on threads other than the main thread on multi-core machines.
// Task t3 (gamma) is executed synchronously on the main thread.
static void Main()
{
Action action = (object obj) =>
{
Console.WriteLine("Task={0}, bj={1}, Thread={2}", Task.CurrentId, obj.ToString(), Thread.CurrentThread.ManagedThreadId);
};
// Construct an unstarted task
Task t1 = new Task(action, "alpha");
// Cosntruct a started task
Task t2 = Task.Factory.StartNew(action, "beta");
// Block the main thread to demonstate that t2 is executing
t2.Wait();
// Launch t1
t1.Start();
Console.WriteLine("t1 has been launched. (Main Thread={0})", Thread.CurrentThread.ManagedThreadId);
// Wait for the task to finish.
// You may optionally provide a timeout interval or a cancellation token
// to mitigate situations when the task takes too long to finish.
t1.Wait();
// Construct an unstarted task
Task t3 = new Task(action, "gamma");
// Run it synchronously
t3.RunSynchronously();
// Although the task was run synchrounously, it is a good practice to wait for it which observes for
// exceptions potentially thrown by that task.
t3.Wait();
Console.ReadLine();
}
}
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28699126/viewspace-756581/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/28699126/viewspace-756581/