您好,登錄后才能下訂單哦!
本篇內容介紹了“C#中的Task.WhenAll和Task.WhenAny方法怎么用”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
Task.WhenAll()、Task.WhenAny()這兩個與Task.WaitALL()、Task.WaitAny()是有區別的,When是異步的,Wait是同步的。
Task.WhenAll():所有提供的任務已完成時,創建將完成的任務。
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(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);異步等待了所有的任務完成。
代碼:
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方法怎么用”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。