这篇文章介绍了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方法的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持得得之家。
沃梦达教程
本文标题为:C#中的Task.WhenAll和Task.WhenAny方法介绍
基础教程推荐
猜你喜欢
- C#控制台实现飞行棋小游戏 2023-04-22
- C#类和结构详解 2023-05-30
- ZooKeeper的安装及部署教程 2023-01-22
- C# List实现行转列的通用方案 2022-11-02
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- C# windows语音识别与朗读实例 2023-04-27
- winform把Office转成PDF文件 2023-06-14
- unity实现动态排行榜 2023-04-27
- 一个读写csv文件的C#类 2022-11-06
- C# 调用WebService的方法 2023-03-09