第十八节:异步补充(CancellationToken终止信号、whenAll、whenAny、yeild return的用法)
一. CancellationToken终止信号
CancelAfter()超时后发出取消信号
Cancel() 发出取消信号
{
CancellationTokenSource cts = new();
cts.CancelAfter(4000); //4s后取消
await Download1Async("http://www.baidu.com", 200, cts.Token);
//写法1:GetStringAsync + IsCancellationRequested 实现取消
static async Task Download1Async(string url, int count, CancellationToken cancellationToken)
{
using HttpClient client = new();
for (int i = 0; i < count; i++)
{
string html = await client.GetStringAsync(url);
Console.WriteLine(html);
if (cancellationToken.IsCancellationRequested)
{
Console.WriteLine("请求取消");
break;
}
}
}
//写法2:GetStringAsync + ThrowIfCancellationRequested()
static async Task Download1Async(string url, int count, CancellationToken cancellationToken)
{
using HttpClient client = new();
for (int i = 0; i < count; i++)
{
string html = await client.GetStringAsync(url);
Console.WriteLine(html);
cancellationToken.ThrowIfCancellationRequested(); //抛异常实现取消
}
}
//写法3:直接传入CancellationToken参数,用于获得提前终止执行的信号。
static async Task Download1Async(string url, int count, CancellationToken cancellationToken)
{
using HttpClient client = new();
for (int i = 0; i < count; i++)
{
string html = await client.GetStringAsync(url, cancellationToken); //直接传入参数实现取消
Console.WriteLine(html);
}
}
}
二. whenAll 和 whenAny
WhenAny,任何一个Task完成,Task就完成
WhenAll,所有Task完成,Task才完成。用于等待多个任务执行结束,但是不在乎它们的执行顺序
Task<string> t1 = File.ReadAllTextAsync("d:/1.txt");
Task<string> t2 = File.ReadAllTextAsync("d:/2.txt");
Task<string> t3 = File.ReadAllTextAsync("d:/3.txt");
string[] results = await Task.WhenAll(t1, t2, t3);
string s1 = results[0];
string s2 = results[1];
string s3 = results[2];
三. yield return 和异步
yield return不仅能够简化数据的返回,而且可以让数据处理“流水线化”,提升性能
/*
执行顺序:
先执行 yield return "ypf1"; → 输出ypf1 → yield return "ypf2"; →输出ypf2→yield return "ypf3";→输出ypf3
*/
foreach (var item in Test1())
{
Console.WriteLine(item);
}
/*
执行顺序:先把Test2执行完,然后依次遍历即可
*/
foreach (var item in Test2())
{
Console.WriteLine(item);
}
static IEnumerable<string> Test1()
{
yield return "ypf1";
yield return "ypf2";
yield return "ypf3";
}
static IEnumerable<string> Test2()
{
List<string> list = new();
list.Add("ypf1");
list.Add("ypf2");
list.Add("ypf3");
return list;
}
!
- 作 者 : Yaopengfei(姚鹏飞)
- 博客地址 : http://www.cnblogs.com/yaopengfei/
- 声 明1 : 如有错误,欢迎讨论,请勿谩骂^_^。
- 声 明2 : 原创博客请在转载时保留原文链接或在文章开头加上本人博客地址,否则保留追究法律责任的权利。
分类:
20-多线程和异步
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
2019-12-25 第二十三节:Asp.Net Core中的几种安全防护