Async Task Types in C#
Async Task Types in C#
Extend async
to support task types that match a specific pattern, in addition to the well known types System.Threading.Tasks.Task
and System.Threading.Tasks.Task<T>
.
Task Type
A task type is a class
or struct
with an associated builder type identified with System.Runtime.CompilerServices.AsyncMethodBuilderAttribute
. The task type may be non-generic, for async methods that do not return a value, or generic, for methods that return a value.
To support await
, the task type must have a corresponding, accessible GetAwaiter()
method that returns an instance of an awaiter type (see C# 7.7.7.1 Awaitable expressions).
[AsyncMethodBuilder(typeof(MyTaskMethodBuilder<>))]
class MyTask<T>
{
public Awaiter<T> GetAwaiter();
}
class Awaiter<T> : INotifyCompletion
{
public bool IsCompleted { get; }
public T GetResult();
public void OnCompleted(Action completion);
}
Builder Type
The builder type is a class
or struct
that corresponds to the specific task type. The builder type has the following public
methods. For non-generic builder types, SetResult()
has no parameters.
class MyTaskMethodBuilder<T>
{
public static MyTaskMethodBuilder<T> Create();
public void Start<TStateMachine>(ref TStateMachine stateMachine)
where TStateMachine : IAsyncStateMachine;
public void SetStateMachine(IAsyncStateMachine stateMachine);
public void SetException(Exception exception);
public void SetResult(T result);
public void AwaitOnCompleted<TAwaiter, TStateMachine>(
ref TAwaiter awaiter, ref TStateMachine stateMachine)
where TAwaiter : INotifyCompletion
where TStateMachine : IAsyncStateMachine;
public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(
ref TAwaiter awaiter, ref TStateMachine stateMachine)
where TAwaiter : ICriticalNotifyCompletion
where TStateMachine : IAsyncStateMachine;
public MyTask<T> Task { get; }
}
Execution
The types above are used by the compiler to generate the code for the state machine of an async
method. (The generated code is equivalent to the code generated for async methods that return Task
, Task<T>
, or void
. The difference is, for those well known types, the builder types are also known to the compiler.)
Builder.Create()
is invoked to create an instance of the builder type.
If the state machine is implemented as a struct
, then builder.SetStateMachine(stateMachine)
is called with a boxed instance of the state machine that the builder can cache if necessary.
builder.Start(ref stateMachine)
is invoked to associate the builder with compiler-generated state machine instance. The builder must call stateMachine.MoveNext()
either in Start()
or after Start()
has returned to advance the state machine. After Start()
returns, the async
method calls builder.Task
for the task to return from the async method.
Each call to stateMachine.MoveNext()
will advance the state machine. If the state machine completes successfully, builder.SetResult()
is called, with the method return value if any. If an exception is thrown in the state machine, builder.SetException(exception)
is called.
If the state machine reaches an await expr
expression, expr.GetAwaiter()
is invoked. If the awaiter implements ICriticalNotifyCompletion
and IsCompleted
is false, the state machine invokes builder.AwaitUnsafeOnCompleted(ref awaiter, ref stateMachine)
. AwaitUnsafeOnCompleted()
should call awaiter.OnCompleted(action)
with an action that calls stateMachine.MoveNext()
when the awaiter completes. Similarly for INotifyCompletion
and builder.AwaitOnCompleted()
.
Overload Resolution
Overload resolution is extended to recognize task types in addition to Task
and Task<T>
.
An async
lambda with no return value is an exact match for an overload candidate parameter of non-generic task type, and an async
lambda with return type T
is an exact match for an overload candidate parameter of generic task type.
Otherwise if an async
lambda is not an exact match for either of two candidate parameters of task types, or an exact match for both, and there is an implicit conversion from one candidate type to the other, the from candidate wins. Otherwise recursively evaluate the types A
and B
within Task1<A>
and Task2<B>
for better match.
Otherwise if an async
lambda is not an exact match for either of two candidate parameters of task types, but one candidate is a more specialized type than the other, the more specialized candidate wins.
相关文章
- C# async/await异步编程深入理解
- C# 5.0 Async Tips and Tricks, Part 1
- C# Under the Hood: async/await (Marko Papic)
- c#中@标志的作用 C#通过序列化实现深表复制 细说并发编程-TPL 大数据量下DataTable To List效率对比 【转载】C#工具类:实现文件操作File的工具类 异步多线程 Async .net 多线程 Thread ThreadPool Task .Net 反射学习
- MVC+Spring.NET+NHibernate .NET SSH框架整合 C# 委托异步 和 async /await 两种实现的异步 如何消除点击按钮时周围出现的白线? Linq中 AsQueryable(), AsEnumerable()和ToList()的区别和用法
- C#订阅与发布标准实现 visual studio code .net 开发 设计模式之☞策略模式 C#字符串转二进制、二进制转字符串 c# 接口的协变和逆变 c# 使用迭代器来创建可枚举类型 博客园首页新随笔联系订阅管理 随笔 - 117 文章 - 0 评论 - 57 c# 创建,加载,修改XML文档
- 通俗易懂,C#如何安全、高效地玩转任何种类的内存之Span。 给萌新的Flexbox简易入门教程 浅谈Quartz定时任务调度 浅谈JavaScript之Event(上篇) 探索JavaScript数组奥秘 【详解JavaScript系列】JavaScript之函数(一) C# Oracle.ManagedDataAccess 批量更新表数据 VS2015常用快捷键总结
- C#7.2——编写安全高效的C#代码 c# 中模拟一个模式匹配及匹配值抽取 走进 LINQ 的世界 移除Excel工作表密码保护小工具含C#源代码 腾讯QQ会员中心g_tk32算法【C#版】
- EF+LINQ事物处理 C# 使用NLog记录日志入门操作 ASP.NET MVC多语言 仿微软网站效果(转) 详解C#特性和反射(一) c# API接受图片文件以Base64格式上传图片 .NET读取json数据并绑定到对象
- C#语法——泛型的多种应用 C#语法——await与async的正确打开方式 C#线程安全使用(五) C#语法——元组类型 好好耕耘 redis和memcached的区别
- c#打包文件解压缩 C#中使用委托、接口、匿名方法、泛型委托实现加减乘除算法 一个简单例子理解C#的协变和逆变 对于过长字符串的大小比对
- c# 把一个匿名对象赋值给一个Object类型的变量后,怎么取这个变量? c# dynamic动态类型和匿名类 详解C# 匿名对象(匿名类型)、var、动态类型 dynamic 深入浅析C#中的var和dynamic
- 浅谈c#的三个高级参数ref out 和Params C#中is与as的区别分析 “登陆”与“登录”有何区别 经典SQL语句大全(绝对的经典)
- C# 字符串拼接性能探索 c#中+、string.Concat、string.Format、StringBuilder.Append四种方式进行字符串拼接时的性能
- 【卷土重来之C#学习笔记】(二)c#编程概述
- C# (江湖小新)- 写个成语故事阅读系统
- 《C#多线程编程实战(原书第2版)》——3.8 使用BackgroundWorker组件
- C# 中的Async 和 Await 的用法详解
- Demystifying Closures, Futures and async-await in Rust–Part 3: Async & Await
- c# 引用与对象举例
- C#接口显示成员用法
- Redis for Windows(C#缓存)安装和使用
- c# 并行循环支持 async
- c# 异步编程demo (async await)
- 《C#零基础入门之百识百例》(五十六)多态介绍 -- 简单工厂模式
- C# 后台访问webapi
- C# 反射(Reflection)技术
- C# Monitor:锁定资源
- C# -join 子句(C# 参考)
- C# ConfigurationManager 类的使用
- C#的几种文件操作方法
- 【C#】用List做动态数组
- C# 生成树 递归和非递归效率对比 5万左右的数据量
- c#代码规则,C#程序中元素的命名规范
- c#类的定义,c#中的关健字,C#标识符