Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Task.WhenAll and Task.WhenAny methods in C #

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to use Task.WhenAll and Task.WhenAny methods in C#". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. brief introduction

Task.WhenAll () and Task.WhenAny () are different from Task.WaitALL () and Task.WaitAny (). When is asynchronous and Wait is synchronous.

Task.WhenAll (): when all provided tasks have been completed, create the tasks that will be completed.

Task.WhenAny (): when any provided task has been completed, create the task that will be completed.

2. Code case Task.WhenAll

Code:

Class Program {public class DownLoadTest {Stopwatch watch = new Stopwatch (); public DownLoadTest () {watch.Start () } public string DownLoadString (string url) {Console.WriteLine (string.Format ("lower {0} starting line: {1Magazine N0} ms", url, watch.Elapsed.TotalMilliseconds); WebClient wc = new WebClient (); string str = wc.DownloadString (url) Console.WriteLine (string.Format ("lower {0} lines end: {1Magne 4ms N0} ms", url, watch.Elapsed.TotalMilliseconds); return str } public async Task DoRunStringAsync (string url) {Console.WriteLine (string.Format ("step-by-step program starts with {0} ms", url, watch.Elapsed.TotalMilliseconds); var task = await DownLoadStringAsync (url) Console.WriteLine (string.Format); 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 "";}}

Running result:

You can see await Task.WhenAll (tasks); wait asynchronously for all tasks to be completed.

Task.WhenAny

Code:

Class Program {public class DownLoadTest {Stopwatch watch = new Stopwatch (); public DownLoadTest () {watch.Start () } public string DownLoadString (string url) {Console.WriteLine (string.Format ("lower {0} starting line: {1Magazine N0} ms", url, watch.Elapsed.TotalMilliseconds); WebClient wc = new WebClient (); string str = wc.DownloadString (url) Console.WriteLine (string.Format ("lower {0} lines end: {1Magne 4ms N0} ms", url, watch.Elapsed.TotalMilliseconds); return str } public async Task DoRunStringAsync (string url) {Console.WriteLine (string.Format ("step-by-step program starts with {0} ms", url, watch.Elapsed.TotalMilliseconds); var task = await DownLoadStringAsync (url) Console.WriteLine (string.Format); 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 "";}}

Running result:

Task.WhenAny (tasks) waits for at least one task to be completed. Here, when the https://www.baidu.com/ is downloaded first, the waiting will be terminated directly.

This is the end of the content of "how to use Task.WhenAll and Task.WhenAny methods in C#". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report