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#的更多相关文章

  1. Await Async Task

    class Program { static void Main(string[] args) { Console.WriteLine("=======Start Main!======== ...

  2. C++ async task

    最近在搞Android 开发,里面多线程的使用比较频繁,java多线程接口很方便. Thread, AysncTask, Handler 这些接口比起posix提供的pthread_create()等 ...

  3. Android菜鸟的成长笔记(13)——异步任务(Async Task)

    原文:[置顶] Android菜鸟的成长笔记(13)——异步任务(Async Task) Android的UI线程主要负责处理用户的事件及图形显示,因此主线程UI不能阻塞,否则会弹出一个ANR(App ...

  4. async/task/await

    async/task/await三组合是.NET Framework 4.5带给.NET开发者的大礼,合理地使用它,可以提高应用程序的吞吐能力. 但是它的使用有点绕人,如果不正确使用,会带来意想不到的 ...

  5. Rx与Async Task的简单对比

    有关Reactive Extensions的介绍可见https://rx.codeplex.com/,总的来说,你可以当它是又一个异步编程的框架,它以观察者模式实现了对数据流的的“订阅”.一个列表,一 ...

  6. async task 异步消息

     async 和 await 是用来定义的异步方法,async  关键字是上下文关键字,原因在于只有当它修饰方法.lambda 表达式或匿名方法时,它才是关键字. 在所有其他上下文中,都会将其解释为标 ...

  7. c# async Task await Result 死锁

    最近项目数据量较大,使用 async Task异步增加执行效率 遇到问题,当前有2个计算非常耗时,现在需要你优化一下,这2个计算并行执行,2个计算执行完成后将2个结果sum返回给用户 当前我是这样实现 ...

  8. 微信小程序中出现Invoking Page() in async task.问题

    在做项目中需要让页面跳到外网,用到了<web-view src=""> </web-view>组件,需要新建一个文件放这个组件,调接口的时候链接连到这个页面 ...

  9. 《C#并发编程经典实例》学习笔记—2.8 处理 async Task 方法的异常

    异常处理一直是所有编程语言不可避免需要考虑的问题,C#的异步方法的异常处理和同步方法并无差别,同样要借助 try catch 语句捕获异常. 首先编写一个抛出异常的方法 static async Ta ...

随机推荐

  1. kubernetes之service

    service出现的动机 Kubernetes Pods 是有生命周期的.他们可以被创建,而且销毁不会再启动. 如果您使用 Deployment 来运行您的应用程序,则它可以动态创建和销毁 Pod. ...

  2. python函数:匿名函数、函数递归与二分法、面向过程编程

    今天主要讲三大部分内容: 一.匿名函数二.函数递归与二分法三.面向过程编程 一.匿名函数: """ 1. 什么时匿名函数 def定义的是有名函数:特点是可以通过名字重复调 ...

  3. 代码检查工具sonarqube介绍

    SonarQube 是一款用于代码质量管理的开源工具,它主要用于管理源代码的质量.通过插件形式,可以支持众多计算机语言. 比如 java, C#, go,C/C++, PL/SQL,Cobol,Jav ...

  4. Linux之top 监视系统任务的工具

    top 监视系统任务的工具: 和ps 相比,top是动态监视系统任务的工具,top 输出的结果是连续的:  top 命令用法及参数: top 调用方法: top 选择参数 参数: -b  以批量模式运 ...

  5. UVa10615 Andy's First Dictionary(集合set)

    这道题主要用到了set容器和stringstream,用起来非常方便,我第一次见识到,觉得十分的炫酷…… 而且,竟然可以将自己写的单词按照字典序排列,真的太酷了. 下面是书上的代码,目前还处于初学状态 ...

  6. Java WEB框架——SSM迈向M之登录

    1.pom.xml 关于pom.xml,<version>等标签先暂时不谈,<dependency> 的作用主要是添加相应的支持包,比如spring,servlet,jdbc等 ...

  7. 【线段树哈希】「Balkan OI 2016」Haker

    1A海星 题目大意 给你一个长度为 $n$ ,由小写字母构成的字符串 $S$ 和 $Q$ 个操作,每个操作是以下 3 种之一: 1 x y k :询问当前字符串从位置 $x$ 到 $y$ 的子串与从位 ...

  8. 搭建zookeeper集群_其中一个报Mode: standalone,另外两个分别是leader和follower

    用3个zookeeper搭建一个zookeeper集群,首先配置好一个zookeeper1,其余两个都是按照zookeeper1复制过来,然后稍微修改 运行集群成功,查看zookeeper状态 可以看 ...

  9. 一篇文章让您了解MQTT

    转载:https://www.jianshu.com/p/de88edf8e023 什么是MQTT ​ MQTT是基于二进制消息的发布/订阅编程模式的消息协议,最早由IBM提出的,如今已经成为OASI ...

  10. kettle案例实现

    案例一.把stu1的数据按id同步到stu2,stu2有相同id则更新数据 在kettle中新建转换 点击左上角文件—新建—转换到核心对象界面,点击输入,找到表输入拖拽到中间 双击表输入,在数据库连接 ...