本文整理汇总了C#中System.Threading.Tasks.TaskStatus枚举的典型用法代码示例。如果您正苦于以下问题:C# TaskStatus枚举的具体用法?C# TaskStatus怎么用?C# TaskStatus使用的例子?那么, 这里精选的枚举代码示例或许可以为您提供帮助。
在下文中一共展示了TaskStatus枚举的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
public class Example
{
public static void Main()
{
var tasks = new List<Task<int>>();
var source = new CancellationTokenSource();
var token = source.Token;
int completedIterations = 0;
for (int n = 0; n <= 19; n++)
tasks.Add(Task.Run( () => { int iterations = 0;
for (int ctr = 1; ctr <= 2000000; ctr++) {
token.ThrowIfCancellationRequested();
iterations++;
}
Interlocked.Increment(ref completedIterations);
if (completedIterations >= 10)
source.Cancel();
return iterations; }, token));
Console.WriteLine("Waiting for the first 10 tasks to complete...\n");
try {
Task.WaitAll(tasks.ToArray());
}
catch (AggregateException) {
Console.WriteLine("Status of tasks:\n");
Console.WriteLine("{0,10} {1,20} {2,14:N0}", "Task Id",
"Status", "Iterations");
foreach (var t in tasks)
Console.WriteLine("{0,10} {1,20} {2,14}",
t.Id, t.Status,
t.Status != TaskStatus.Canceled ? t.Result.ToString("N0") : "n/a");
}
}
}
输出:
Waiting for the first 10 tasks to complete... Status of tasks: Task Id Status Iterations 1 RanToCompletion 2,000,000 2 RanToCompletion 2,000,000 3 RanToCompletion 2,000,000 4 RanToCompletion 2,000,000 5 RanToCompletion 2,000,000 6 RanToCompletion 2,000,000 7 RanToCompletion 2,000,000 8 RanToCompletion 2,000,000 9 RanToCompletion 2,000,000 10 Canceled n/a 11 Canceled n/a 12 Canceled n/a 13 Canceled n/a 14 Canceled n/a 15 Canceled n/a 16 RanToCompletion 2,000,000 17 Canceled n/a 18 Canceled n/a 19 Canceled n/a 20 Canceled n/a