TaskStatus 列舉
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表 Task 開發週期中的目前階段。
public enum class TaskStatus
public enum TaskStatus
type TaskStatus =
Public Enum TaskStatus
- 繼承
欄位
Canceled | 6 | 工作確認取消動作,不論是因為工作在語彙基元處於信號狀態時使用自己的 CancellationToken 擲回 OperationCanceledException,或是工作的 CancellationToken 信號在工作開始執行之前便已存在。 如需詳細資訊,請參閱工作取消。 |
Created | 0 | 工作已初始化但尚未排程。 |
Faulted | 7 | 工作因未處理的例外狀況而完成。 |
RanToCompletion | 5 | 工作已成功完成執行。 |
Running | 3 | 工作正在執行,但尚未完成。 |
WaitingForActivation | 1 | 工作正在等候由 .NET 基礎結構從內部啟動並排程。 |
WaitingForChildrenToComplete | 4 | 工作已完成執行,而且在暗中等候附加的子工作完成。 |
WaitingToRun | 2 | 工作已排定執行,但尚未開始執行。 |
範例
下列範例會建立 20 個工作,直到計數器遞增為 2 百萬個值為止。 當前 10 個工作達到 2 百萬個時,取消了取消權杖,而且任何計數器尚未達到 2 百萬的工作都取消。 然後,此範例會 Task.Status 檢查每個工作的 屬性,以指出工作是否已順利完成或已取消。 針對已完成的專案,它會顯示工作所傳回的值。
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");
}
}
}
// The example displays output like the following:
// 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
Imports System.Collections.Generic
Imports System.Threading
Imports System.Threading.Tasks
Module Example
Public Sub Main()
Dim tasks As New List(Of Task(Of Integer))()
Dim source As New CancellationTokenSource
Dim token As CancellationToken = source.Token
Dim completedIterations As Integer = 0
For n As Integer = 0 To 19
tasks.Add(Task.Run( Function()
Dim iterations As Integer= 0
For ctr As Long = 1 To 2000000
token.ThrowIfCancellationRequested()
iterations += 1
Next
Interlocked.Increment(completedIterations)
If completedIterations >= 10 Then source.Cancel()
Return iterations
End Function, token))
Next
Console.WriteLine("Waiting for the first 10 tasks to complete... ")
Try
Task.WaitAll(tasks.ToArray())
Catch e As AggregateException
Console.WriteLine("Status of tasks:")
Console.WriteLine()
Console.WriteLine("{0,10} {1,20} {2,14}", "Task Id",
"Status", "Iterations")
For Each t In tasks
Console.WriteLine("{0,10} {1,20} {2,14}",
t.Id, t.Status,
If(t.Status <> TaskStatus.Canceled,
t.Result.ToString("N0"), "n/a"))
Next
End Try
End Sub
End Module
' The example displays output like the following:
' 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
備註
屬性 Task.Status 會傳回 列舉的成員 TaskStatus ,以指出工作的目前狀態。