本篇内容介绍了“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 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/先下载完,就直接终止等待 。
“C#中的Task.WhenAll和Task.WhenAny方法怎么用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!