提供任务并行库相关类型
System.Threading.Tasks
定义
描述
包含用于简化异步编程和并行计算的类,基于 TPL (Task Parallel Library)
参数
参数名 | 类型 | 描述 |
---|---|---|
action | Action | 要执行的操作 |
返回值
返回值 | 类型 | 描述 |
---|---|---|
task | Task | 表示异步操作的任务对象 |
示例
示例代码,异步任务执行:
using System.Threading.Tasks;
class Program {
static async Task Main() {
Task<int> task = CalculateAsync();
int result = await task;
Console.WriteLine(result);
}
static async Task<int> CalculateAsync() {
await Task.Delay(1000);
return 42;
}
}