一、简介
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方法的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持编程网。