C#中的Task.WhenAll和Task.WhenAny方法介绍
短信预约 -IT技能 免费直播动态提醒
一、简介
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方法的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持编程网。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341