AsyncStreamsInCShaper 8.0

C# 8.0中支持异步返回枚举类型async Task<IEnumerable<T>>

sync Streams这个功能已经发布很久了,在去年的Build 2018 The future of C#就有演示

C# 5引入了 Async/Await,用以提高用户界面响应能力和对 Web 资源的访问能力。换句话说,异步方法用于执行不阻塞线程并返回一个标量结果的异步操作。

常规示例

要了解问什么需要Async Streams,我们先来看看这样的一个示例,求出5以内的整数的和.

static int SumFromOneToCount(int count)

{

ConsoleExt.WriteLine("SumFromOneToCount called!");

var sum = 0;

for (var i = 0; i <= count; i++)

{

sum = sum + i;

}

return sum;

}

调用方法.

static void Main(string[] args)

{

const int count = 5;

ConsoleExt.WriteLine($"Starting the application with count: {count}!");

ConsoleExt.WriteLine("Classic sum starting.");

ConsoleExt.WriteLine($"Classic sum result: {SumFromOneToCount(count)}");

ConsoleExt.WriteLine("Classic sum completed.");

ConsoleExt.WriteLine("################################################");

}

输出结果.

可以看到,整个过程就一个线程Id为1的线程自上而下执行,这是最基础的做法.

Yield Return

接下来,我们使用yield运算符使得这个方法编程延迟加载,如下所示.

static IEnumerable<int> SumFromOneToCountYield(int count)

{

ConsoleExt.WriteLine("SumFromOneToCountYield called!");

var sum = 0;

for (var i = 0; i <= count; i++)

{

sum = sum + i;

yield return sum;

}

}

主函数

static void Main(string[] args)

{

const int count = 5;

ConsoleExt.WriteLine("Sum with yield starting.");

foreach (var i in SumFromOneToCountYield(count))

{

ConsoleExt.WriteLine($"Yield sum: {i}");

}

ConsoleExt.WriteLine("Sum with yield completed.");

ConsoleExt.WriteLine("################################################");

ConsoleExt.WriteLine(Environment.NewLine);

}

运行结果如下.

正如你在输出窗口中看到的那样,结果被分成几个部分返回,而不是作为一个值返回。以上显示的累积结果被称为惰性枚举。但是,仍然存在一个问题,即 sum 方法阻塞了代码的执行。如果你查看线程ID,可以看到所有东西都在主线程1中运行,这显然不完美,继续改造.

Async Return

我们试着将async用于SumFromOneToCount方法(没有yield关键字).

static async Task<int> SumFromOneToCountAsync(int count)

{

ConsoleExt.WriteLine("SumFromOneToCountAsync called!");

var result = await Task.Run(() =>

{

var sum = 0;

for (var i = 0; i <= count; i++)

{

sum = sum + i;

}

return sum;

});

return result;

}

主函数

static async Task Main(string[] args)

{

const int count = 5;

ConsoleExt.WriteLine("async example starting.");

// Sum runs asynchronously! Not enough. We need sum to be async with lazy behavior.

var result = await SumFromOneToCountAsync(count);

ConsoleExt.WriteLine("async Result: " + result);

ConsoleExt.WriteLine("async completed.");

ConsoleExt.WriteLine("################################################");

ConsoleExt.WriteLine(Environment.NewLine);

}

运行结果.

我们可以看到计算过程是在另一个线程中运行,但结果仍然是作为一个值返回!任然不完美.

如果我们想把惰性枚举(yield return)与异步方法结合起来,即返回Task<IEnumerable,这怎么实现呢?

IAsyncEnumerable

其实,在C# 8.0中Task<IEnumerable>这种组合称为IAsyncEnumerable。

这个新功能为我们提供了一种很好的技术来解决拉异步延迟加载的问题,例如从网站下载数据或从文件或数据库中读取记录,与 IEnumerable 和 IEnumerator 类似,Async Streams 提供了两个新接口 IAsyncEnumerable 和 IAsyncEnumerator,定义如下:

public interface IAsyncEnumerable<out T>

{

IAsyncEnumerator<T> GetAsyncEnumerator();

}

public interface IAsyncEnumerator<out T> : IAsyncDisposable

{

Task<bool> MoveNextAsync();

T Current { get; }

}

// Async Streams Feature 可以被异步销毁

public interface IAsyncDisposable

{

Task DiskposeAsync();

}

AsyncStream

下面,我们就来见识一下AsyncStrema的威力,我们使用IAsyncEnumerable来对函数进行改造,如下.

static async Task ConsumeAsyncSumSeqeunc(IAsyncEnumerable<int> sequence)

{

ConsoleExt.WriteLineAsync("ConsumeAsyncSumSeqeunc Called");

await foreach (var value in sequence)

{

ConsoleExt.WriteLineAsync($"Consuming the value: {value}");

// simulate some delay!

await Task.Delay(TimeSpan.FromSeconds(1));

};

}

private static async IAsyncEnumerable<int> ProduceAsyncSumSeqeunc(int count)

{

ConsoleExt.WriteLineAsync("ProduceAsyncSumSeqeunc Called");

var sum = 0;

for (var i = 0; i <= count; i++)

{

sum = sum + i;

// simulate some delay!

await Task.Delay(TimeSpan.FromSeconds(0.5));

yield return sum;

}

}

主函数

static async Task Main(string[] args)

{

const int count = 5;

ConsoleExt.WriteLine("Starting Async Streams Demo!");

// Start a new task. Used to produce async sequence of data!

IAsyncEnumerable<int> pullBasedAsyncSequence = ProduceAsyncSumSeqeunc(count);

// Start another task; Used to consume the async data sequence!

var consumingTask = Task.Run(() => ConsumeAsyncSumSeqeunc(pullBasedAsyncSequence));

await Task.Delay(TimeSpan.FromSeconds(3));

ConsoleExt.WriteLineAsync("X#X#X#X#X#X#X#X#X#X# Doing some other work X#X#X#X#X#X#X#X#X#X#");

// Just for demo! Wait until the task is finished!

await consumingTask;

ConsoleExt.WriteLineAsync("Async Streams Demo Done!");

}

如果一切顺利,那么就能看到这样的运行结果了.

最后,看到这就是我们想要的结果,在枚举的基础上,进行了异步迭代.

可以看到,整个计算过程并没有造成主线程的阻塞,其中,值得重点关注的是红色方框区域的线程5!线程5!线程5!线程5在请求下一个结果后,并没有等待结果返回,而是去了Main()函数中做了别的事情,等待请求的结果返回后,线程5又接着执行foreach中任务.

我们已经讨论过 Async Streams,它是一种出色的异步拉取技术,可用于进行生成多个值的异步计算。

Async Streams 背后的编程概念是异步拉取模型。我们请求获取序列的下一个元素,并最终得到答复。

Async Streams 提供了一种处理异步数据源的绝佳方法,希望对大家能够有所帮助。

C#8.0中的 await foreach的更多相关文章

  1. 聊一聊C# 8.0中的await foreach

    AsyncStreamsInCShaper8.0 很开心今天能与大家一起聊聊C# 8.0中的新特性-Async Streams,一般人通常看到这个词表情是这样. 简单说,其实就是C# 8.0中支持aw ...

  2. 小心C# 5.0 中的await and async模式造成的死锁

    平时在使用C# 5.0中的await and async关键字的时候总是没注意,直到今天在调试一个ASP.NET项目时,发现在调用一个声明为async的方法后,程序老是莫名其妙的被卡住,就算声明为as ...

  3. [转]小心C# 5.0 中的await and async模式造成的死锁

    原文链接 https://www.cnblogs.com/OpenCoder/p/4434574.html 内容 UI Example Consider the example below. A bu ...

  4. VS2015 C#6.0 中的那些新特性(转载)

    自动属性初始化 (Initializers for auto-properties) 以前我们是这么写的 为一个默认值加一个后台字段是不是很不爽,现在我们可以这样写 只读属性的初始化(Getter-o ...

  5. C#6.0 中的那些新特性

    C#6.0 中的那些新特性 前言 VS2015在自己机器上确实是装好了,费了老劲了,想来体验一下跨平台的快感,结果被微软狠狠的来了一棒子了,装好了还是没什么用,应该还需要装Xarmain插件,配置一些 ...

  6. [译] C# 5.0 中的 Async 和 Await (整理中...)

    C# 5.0 中的 Async 和 Await [博主]反骨仔 [本文]http://www.cnblogs.com/liqingwen/p/6069062.html 伴随着 .NET 4.5 和 V ...

  7. [C#] .NET4.0中使用4.5中的 async/await 功能实现异

    好东西需要分享 原文出自:http://www.itnose.net/detail/6091186.html 在.NET Framework 4.5中添加了新的异步操作库,但是在.NET Framew ...

  8. C#同步,异步的理解,包括5.0中await和async(学习笔记)

    之前在工作中一直用的是同步线程,就是先进入画面的load事件,然后在里面进行数据库调用的处理.后面又遇到了公司软件中一些比较古老的代码,一开始在那块古老代码中增加机能的时候,我想用到数据库的数据给画面 ...

  9. [C#] .NET4.0中使用4.5中的 async/await 功能实现异步

    在.NET Framework 4.5中添加了新的异步操作库,但是在.NET Framework 4.0中却无法使用.这时不免面临着抉择,到底是升级整个解决方案还是不使用呢? 如果你的软件还没发布出去 ...

随机推荐

  1. 关于c# 发射的调用并进行缓存

    private static object CreateObject(string AssemblyPath, string classNamespace) { object objObject = ...

  2. 用jQuery屏蔽掉按回车键时提交表单

    <script type="text/javascript"> $(function() { $("input").keypress(functio ...

  3. 空间管理 您的位置: 51Testing软件测试网 » lilisx2006的个人空间 » 日志 在一个没有测试经理的小公司如何做好测试

    如何在一个没有测试经理的小公司做好测试? 首先,没有测试经理意味着测试人员没有最直接的管理者,往往这种时候的管理者是开发经理或技术总监,但他们何其忙耶?同时,在无人监管的情况下,测试是一个很容易偷懒的 ...

  4. 数据库 proc编程八

    #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <stri ...

  5. 【转】MFC WM_USER和WM_APP

    WM_USER常量是Windows帮助应用程序定义私有窗口类里的私有消息,通常使用WM_USER+一个整数值,但总值不能超过0x7FFF. #define WM_USER       0x0400 W ...

  6. e1084. 捕获错误和异常

    All errors and exceptions extend from Throwable. By catching Throwable, it is possible to handle all ...

  7. html -- 实体

  8. R语言低级绘图函数-abline

    abline 函数的作用是在一张图表上添加直线, 可以是一条斜线,通过x或y轴的交点和斜率来确定位置:也可以是一条水平或者垂直的线,只需要指定与x轴或y轴交点的位置就可以了 常见用法: 1)添加直线 ...

  9. 【Java面试题】13 Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以implements(实现)interface(接口)?

    1.什么是匿名内部类? 内部类,存在于另一个类内部的类,而匿名内部类,顾名思义,就是没有名字的内部类. 2.为什么需要匿名内部类? 每个inner class都能够各自继承某一实现类(implemen ...

  10. kafka学习之-深入研究原理

    参考博客: http://www.cnblogs.com/fxjwind --阿里牛人 http://blog.csdn.net/lizhitao/article/details/41778193   ...