本文整理汇总了C#中System.Threading.Tasks.Task.WhenAll方法的典型用法代码示例。如果您正苦于以下问题:C# Task.WhenAll方法的具体用法?C# Task.WhenAll怎么用?C# Task.WhenAll使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Threading.Tasks.Task
的用法示例。
在下文中一共展示了Task.WhenAll方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
public class Example
{
public static void Main()
{
var tasks = new List<Task<long>>();
for (int ctr = 1; ctr <= 10; ctr++) {
int delayInterval = 18 * ctr;
tasks.Add(Task.Run(async () => { long total = 0;
await Task.Delay(delayInterval);
var rnd = new Random();
// Generate 1,000 random numbers.
for (int n = 1; n <= 1000; n++)
total += rnd.Next(0, 1000);
return total; } ));
}
var continuation = Task.WhenAll(tasks);
try {
continuation.Wait();
}
catch (AggregateException)
{ }
if (continuation.Status == TaskStatus.RanToCompletion) {
long grandTotal = 0;
foreach (var result in continuation.Result) {
grandTotal += result;
Console.WriteLine("Mean: {0:N2}, n = 1,000", result/1000.0);
}
Console.WriteLine("\nMean of Means: {0:N2}, n = 10,000",
grandTotal/10000);
}
// Display information on faulted tasks.
else {
foreach (var t in tasks) {
Console.WriteLine("Task {0}: {1}", t.Id, t.Status);
}
}
}
}
输出:
Mean: 506.34, n = 1,000 Mean: 504.69, n = 1,000 Mean: 489.32, n = 1,000 Mean: 505.96, n = 1,000 Mean: 515.31, n = 1,000 Mean: 499.94, n = 1,000 Mean: 496.92, n = 1,000 Mean: 508.58, n = 1,000 Mean: 494.88, n = 1,000 Mean: 493.53, n = 1,000 Mean of Means: 501.55, n = 10,000
示例2: Main
//引入命名空间
using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Threading;
using System.Threading.Tasks;
public class Example
{
public static void Main()
{
int failed = 0;
var tasks = new List<Task>();
String[] urls = { "www.adatum.com", "www.cohovineyard.com",
"www.cohowinery.com", "www.northwindtraders.com",
"www.contoso.com" };
foreach (var value in urls) {
var url = value;
tasks.Add(Task.Run( () => { var png = new Ping();
try {
var reply = png.Send(url);
if (! (reply.Status == IPStatus.Success)) {
Interlocked.Increment(ref failed);
throw new TimeoutException("Unable to reach " + url + ".");
}
}
catch (PingException) {
Interlocked.Increment(ref failed);
throw;
}
}));
}
Task t = Task.WhenAll(tasks);
try {
t.Wait();
}
catch {}
if (t.Status == TaskStatus.RanToCompletion)
Console.WriteLine("All ping attempts succeeded.");
else if (t.Status == TaskStatus.Faulted)
Console.WriteLine("{0} ping attempts failed", failed);
}
}
输出:
5 ping attempts failed
示例3: Main
//引入命名空间
using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Threading;
using System.Threading.Tasks;
public class Example
{
public static async Task Main()
{
int failed = 0;
var tasks = new List<Task>();
String[] urls = { "www.adatum.com", "www.cohovineyard.com",
"www.cohowinery.com", "www.northwindtraders.com",
"www.contoso.com" };
foreach (var value in urls) {
var url = value;
tasks.Add(Task.Run( () => { var png = new Ping();
try {
var reply = png.Send(url);
if (! (reply.Status == IPStatus.Success)) {
Interlocked.Increment(ref failed);
throw new TimeoutException("Unable to reach " + url + ".");
}
}
catch (PingException) {
Interlocked.Increment(ref failed);
throw;
}
}));
}
Task t = Task.WhenAll(tasks.ToArray());
try {
await t;
}
catch {}
if (t.Status == TaskStatus.RanToCompletion)
Console.WriteLine("All ping attempts succeeded.");
else if (t.Status == TaskStatus.Faulted)
Console.WriteLine("{0} ping attempts failed", failed);
}
}
输出:
5 ping attempts failed
示例4: Main
//引入命名空间
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
public class Example
{
public static void Main()
{
var tasks = new Task<long>[10];
for (int ctr = 1; ctr <= 10; ctr++) {
int delayInterval = 18 * ctr;
tasks[ctr - 1] = Task.Run(async () => { long total = 0;
await Task.Delay(delayInterval);
var rnd = new Random();
// Generate 1,000 random numbers.
for (int n = 1; n <= 1000; n++)
total += rnd.Next(0, 1000);
return total; } );
}
var continuation = Task.WhenAll(tasks);
try {
continuation.Wait();
}
catch (AggregateException)
{}
if (continuation.Status == TaskStatus.RanToCompletion) {
long grandTotal = 0;
foreach (var result in continuation.Result) {
grandTotal += result;
Console.WriteLine("Mean: {0:N2}, n = 1,000", result/1000.0);
}
Console.WriteLine("\nMean of Means: {0:N2}, n = 10,000",
grandTotal/10000);
}
// Display information on faulted tasks.
else {
foreach (var t in tasks)
Console.WriteLine("Task {0}: {1}", t.Id, t.Status);
}
}
}
输出:
Mean: 506.38, n = 1,000 Mean: 501.01, n = 1,000 Mean: 505.36, n = 1,000 Mean: 492.00, n = 1,000 Mean: 508.36, n = 1,000 Mean: 503.99, n = 1,000 Mean: 504.95, n = 1,000 Mean: 508.58, n = 1,000 Mean: 490.23, n = 1,000 Mean: 501.59, n = 1,000 Mean of Means: 502.00, n = 10,000