Task.WhenAll和Task.WhenAny
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 Task<string> DoRunStringAsync(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<string> 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<string> DoRunsAsync(IEnumerable<string> urls) { DownLoadTest dwtest = new DownLoadTest(); List<Task> tasks = new List<Task>(); 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 Task<string> DoRunStringAsync(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<string> 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<string> DoRunsAsync(IEnumerable<string> urls) { DownLoadTest dwtest = new DownLoadTest(); List<Task> tasks = new List<Task>(); 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/先下载完,就直接终止等待 。
本文来自博客园,作者:码农阿亮,转载请注明原文链接:https://www.cnblogs.com/wml-it/p/14862082.html
技术的发展日新月异,随着时间推移,无法保证本博客所有内容的正确性。如有误导,请大家见谅,欢迎评论区指正!
开源库地址,欢迎点亮:
GitHub:https://github.com/ITMingliang
Gitee: https://gitee.com/mingliang_it
GitLab: https://gitlab.com/ITMingliang
建群声明: 本着技术在于分享,方便大家交流学习的初心,特此建立【编程内功修炼交流群】,为大家答疑解惑。热烈欢迎各位爱交流学习的程序员进群,也希望进群的大佬能不吝分享自己遇到的技术问题和学习心得!进群方式:扫码关注公众号,后台回复【进群】。