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.
Async Task Types in C#的更多相关文章
- Await Async Task
class Program { static void Main(string[] args) { Console.WriteLine("=======Start Main!======== ...
- C++ async task
最近在搞Android 开发,里面多线程的使用比较频繁,java多线程接口很方便. Thread, AysncTask, Handler 这些接口比起posix提供的pthread_create()等 ...
- Android菜鸟的成长笔记(13)——异步任务(Async Task)
原文:[置顶] Android菜鸟的成长笔记(13)——异步任务(Async Task) Android的UI线程主要负责处理用户的事件及图形显示,因此主线程UI不能阻塞,否则会弹出一个ANR(App ...
- async/task/await
async/task/await三组合是.NET Framework 4.5带给.NET开发者的大礼,合理地使用它,可以提高应用程序的吞吐能力. 但是它的使用有点绕人,如果不正确使用,会带来意想不到的 ...
- Rx与Async Task的简单对比
有关Reactive Extensions的介绍可见https://rx.codeplex.com/,总的来说,你可以当它是又一个异步编程的框架,它以观察者模式实现了对数据流的的“订阅”.一个列表,一 ...
- async task 异步消息
async 和 await 是用来定义的异步方法,async 关键字是上下文关键字,原因在于只有当它修饰方法.lambda 表达式或匿名方法时,它才是关键字. 在所有其他上下文中,都会将其解释为标 ...
- c# async Task await Result 死锁
最近项目数据量较大,使用 async Task异步增加执行效率 遇到问题,当前有2个计算非常耗时,现在需要你优化一下,这2个计算并行执行,2个计算执行完成后将2个结果sum返回给用户 当前我是这样实现 ...
- 微信小程序中出现Invoking Page() in async task.问题
在做项目中需要让页面跳到外网,用到了<web-view src=""> </web-view>组件,需要新建一个文件放这个组件,调接口的时候链接连到这个页面 ...
- 《C#并发编程经典实例》学习笔记—2.8 处理 async Task 方法的异常
异常处理一直是所有编程语言不可避免需要考虑的问题,C#的异步方法的异常处理和同步方法并无差别,同样要借助 try catch 语句捕获异常. 首先编写一个抛出异常的方法 static async Ta ...
随机推荐
- PHP敏感词替换
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8& ...
- vue报错Maximum call stack size exceeded at abort (webpack-internal:///./node_modules/_vue-router@3.1.3@vue-router/dist/vue-router.esm.js:2079)
报错原因: import cellDetail from '@/components/common/dialog/cellDetail.vue'; 解决方法: import celldetail fr ...
- 1 C# 将对象序列化
public static string ObjectToXml(object obj) { using (MemoryStream memoryStream = new MemoryStream() ...
- OPNsense防火墙搭建实验环境,MSF与SSH进行流量转发
OPNsense防火墙搭建实验环境,MSF与SSH进行流量转发 摘要: 记录实验过程中踩到的坑.介绍OPNsense防火墙的安装配置并搭建实验环境,利用msf的模块及ssh进行流量转发(LAN向DMZ ...
- deep_learning_cross_entropy
交叉熵损失函数 交叉熵代价函数(Cross-entropy cost function)是用来衡量人工神经网络(ANN)的预测值与实际值的一种方式.与二次代价函数相比,它能更有效地促进ANN的训练.在 ...
- Ubuntu 16.04 装机后如何永久更改ulimit和修改MySQL的存储路径datadir
Ubuntu 16.04 装机后的配置要点: 1. 网络的配置 2. 更改源列表 3. 永久更改ulimit ulimit限制着程序打开文件的数目,默认情况下为1024,作为服务器使用时,这个数字往往 ...
- 4.Shell 判断用户的参数
1.Shell 判断用户的参数 系统在执行mkdir命令时会判断用户输入的信息,即判断用户指定的文件夹名称是否已经存在,如果存在则提示报错:反之则自动创建. Shell脚本中的条件测试语法可以判断表达 ...
- python+Appium自动化:各种元素定位方法
name定位 driver.find_element_by_name('飞利浦净水').click() 测试结果报错:selenium.common.exceptions.InvalidSelecto ...
- 留言条.html .js来完成
let data = [{ username: "张桑0", time: "2017-09-07 12:00:00", text: "留言文本内容.& ...
- python标准库之glob
在python中,glob模块是用来查找匹配的文件的 在查找的条件中,需要用到Unix shell中的匹配规则: * : 匹配所所有 ? : 匹配一个字符 *.* : 匹配如:[hello.txt,c ...