C#中的Task.WhenAll和Task.WhenAny方法介绍
一、简介
Task.WhenAll()、Task.WhenAny()这两个与Task.WaitALL()、Task.WaitAny()是有区别的,When是异步的,Wait是同步的。
Task.WhenAll():所有提供的任务已完成时,创建将完成的任务。
Task.WhenAny():任何提供的任务已完成时,创建将完成的任务。
二、代码案例
Task.WhenAll
代码:
class Program { public class DownLoadTest { Stopwatch watch = new Stopwatch(); public DownLoadTest() { watch.Start(); } public string DownLoadString(string url) { Console.WriteLine(string.Format("下載{0}開始運行 :{1,4:N0}ms", url, watch.Elapsed.TotalMilliseconds)); WebClient wc = new WebClient(); string str = wc.DownloadString(url); Console.WriteLine(string.Format("下載{0}運行結束 :{1,4:N0}ms", url, watch.Elapsed.TotalMilliseconds)); return str; } public async TaskDoRunStringAsync(string url) { Console.WriteLine(string.Format("異步程序獲取{0}開始運行:{1,4:N0}ms", url, watch.Elapsed.TotalMilliseconds)); var task = await DownLoadStringAsync(url); Console.WriteLine(string.Format("異步程序獲取{0運行結束:{1,4:N0}ms", url, watch.Elapsed.TotalMilliseconds)); return task; } public async Task DownLoadStringAsync(string url) { string str = await Task.Run(() => DownLoadString(url)); return str; } } static void Main(string[] args) { string[] urls = { "https://www.baidu.com/", "https://www.taobao.com/", "https://www.cnblogs.com/" }; var task = DoRunsAsync(urls); //task.Wait(3000); Console.WriteLine(string.Format("主線程運行結束.")); Console.ReadKey(); } private static async Task DoRunsAsync(IEnumerable urls) { DownLoadTest dwtest = new DownLoadTest(); List tasks = new List (); foreach (var url in urls) { var task = dwtest.DoRunStringAsync(url); Console.WriteLine("task.Id =" + task.Id + " url=" + url); tasks.Add(task); } await Task.WhenAll(tasks); foreach (var task in tasks) { Console.WriteLine("task.Id =" + task.Id + " task.Status=" + task.Status); } return ""; } }
运行结果:
可以看到await Task.WhenAll(tasks);异步等待了所有的任务完成。
Task.WhenAny
代码:
class Program { public class DownLoadTest { Stopwatch watch = new Stopwatch(); public DownLoadTest() { watch.Start(); } public string DownLoadString(string url) { Console.WriteLine(string.Format("下載{0}開始運行 :{1,4:N0}ms", url, watch.Elapsed.TotalMilliseconds)); WebClient wc = new WebClient(); string str = wc.DownloadString(url); Console.WriteLine(string.Format("下載{0}運行結束 :{1,4:N0}ms", url, watch.Elapsed.TotalMilliseconds)); return str; } public async TaskDoRunStringAsync(string url) { Console.WriteLine(string.Format("異步程序獲取{0}開始運行:{1,4:N0}ms", url, watch.Elapsed.TotalMilliseconds)); var task = await DownLoadStringAsync(url); Console.WriteLine(string.Format("異步程序獲取{0運行結束:{1,4:N0}ms", url, watch.Elapsed.TotalMilliseconds)); return task; } public async Task DownLoadStringAsync(string url) { string str = await Task.Run(() => DownLoadString(url)); return str; } } static void Main(string[] args) { string[] urls = { "https://www.baidu.com/", "https://www.taobao.com/", "https://www.cnblogs.com/" }; var task = DoRunsAsync(urls); task.Wait(1000); Console.WriteLine(string.Format("主線程運行結束.")); Console.ReadKey(); } private static async Task DoRunsAsync(IEnumerable urls) { DownLoadTest dwtest = new DownLoadTest(); List tasks = new List (); foreach (var url in urls) { var task = dwtest.DoRunStringAsync(url); Console.WriteLine("task.Id =" + task.Id + " url=" + url); tasks.Add(task); } await Task.WhenAny(tasks); foreach (var task in tasks) { Console.WriteLine("task.Id =" + task.Id + " task.Status=" + task.Status); } return ""; } }
运行结果:
Task.WhenAny(tasks)至少等待了一个任务的完成。这里https://www.baidu.com/先下载完,就直接终止等待 。
到此这篇关于C#中Task.WhenAll和Task.WhenAny方法的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
X 关闭
X 关闭
- 15G资费不大降!三大运营商谁提供的5G网速最快?中国信通院给出答案
- 2联想拯救者Y70发布最新预告:售价2970元起 迄今最便宜的骁龙8+旗舰
- 3亚马逊开始大规模推广掌纹支付技术 顾客可使用“挥手付”结账
- 4现代和起亚上半年出口20万辆新能源汽车同比增长30.6%
- 5如何让居民5分钟使用到各种设施?沙特“线性城市”来了
- 6AMD实现连续8个季度的增长 季度营收首次突破60亿美元利润更是翻倍
- 7转转集团发布2022年二季度手机行情报告:二手市场“飘香”
- 8充电宝100Wh等于多少毫安?铁路旅客禁止、限制携带和托运物品目录
- 9好消息!京东与腾讯续签三年战略合作协议 加强技术创新与供应链服务
- 10名创优品拟通过香港IPO全球发售4100万股 全球发售所得款项有什么用处?