Task.ContinueWith メソッド
定義
オーバーロード
ContinueWith(Action<Task,Object>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler) |
呼び出し元が提供した状態情報およびキャンセル トークンを受け取り、対象の Task の完了時に実行される継続タスクを作成します。Creates a continuation that receives caller-supplied state information and a cancellation token and that executes when the target Task completes. 継続タスクは、指定した一連の条件に基づいて実行され、指定したスケジューラが使用されます。The continuation executes based on a set of specified conditions and uses a specified scheduler. |
ContinueWith(Action<Task>, CancellationToken, TaskContinuationOptions, TaskScheduler) |
対象のタスクが完了したときに、指定した TaskContinuationOptions に従って実行される継続タスクを作成します。Creates a continuation that executes when the target task competes according to the specified TaskContinuationOptions. この継続タスクは、キャンセル トークンを受け取り、指定されたスケジューラを使用します。The continuation receives a cancellation token and uses a specified scheduler. |
ContinueWith(Action<Task,Object>, Object, TaskScheduler) |
呼び出し元が提供した状態情報を受け取り、対象の Task が完了したときに非同期的に実行される継続タスクを作成します。Creates a continuation that receives caller-supplied state information and executes asynchronously when the target Task completes. 継続タスクは、指定されたスケジューラを使用します。The continuation uses a specified scheduler. |
ContinueWith(Action<Task,Object>, Object, TaskContinuationOptions) |
呼び出し元が提供した状態情報を受け取り、対象の Task が完了したときに実行される継続タスクを作成します。Creates a continuation that receives caller-supplied state information and executes when the target Task completes. 継続タスクは、指定した一連の条件に基づいて実行されます。The continuation executes based on a set of specified conditions. |
ContinueWith(Action<Task,Object>, Object, CancellationToken) |
呼び出し元が提供した状態情報およびキャンセル トークンを受け取り、対象の Task の完了時に非同期的に実行される継続タスクを作成します。Creates a continuation that receives caller-supplied state information and a cancellation token and that executes asynchronously when the target Task completes. |
ContinueWith(Action<Task,Object>, Object) |
呼び出し元が提供した状態情報を受け取り、対象の Task が完了したときに実行される継続タスクを作成します。Creates a continuation that receives caller-supplied state information and executes when the target Task completes. |
ContinueWith(Action<Task>, TaskContinuationOptions) |
対象のタスクが完了したときに、指定した TaskContinuationOptions に従って実行される継続タスクを作成します。Creates a continuation that executes when the target task completes according to the specified TaskContinuationOptions. |
ContinueWith(Action<Task>, CancellationToken) |
キャンセル トークンを受け取って、対象の Task が完了したときに非同期的に実行される継続タスクを作成します。Creates a continuation that receives a cancellation token and executes asynchronously when the target Task completes. |
ContinueWith(Action<Task>) |
ターゲットの Task が完了したときに非同期に実行する継続タスクを作成します。Creates a continuation that executes asynchronously when the target Task completes. |
ContinueWith(Action<Task>, TaskScheduler) |
ターゲットの Task が完了したときに非同期に実行する継続タスクを作成します。Creates a continuation that executes asynchronously when the target Task completes. 継続タスクは、指定されたスケジューラを使用します。The continuation uses a specified scheduler. |
ContinueWith<TResult>(Func<Task,Object,TResult>, Object, TaskContinuationOptions) |
対象の Task が完了したときに、指定したタスク継続オプションに基づいて実行される継続タスクを作成します。Creates a continuation that executes based on the specified task continuation options when the target Task completes. この継続タスクは、呼び出し元が提供した状態情報を受け取ります。The continuation receives caller-supplied state information. |
ContinueWith<TResult>(Func<Task,Object,TResult>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler) |
対象の Task が完了したときに、指定したタスク継続オプションに基づいて実行され、値を返す継続タスクを作成します。Creates a continuation that executes based on the specified task continuation options when the target Task completes and returns a value. この継続タスクは、呼び出し元が提供した状態情報とキャンセル トークンを受け取り、指定したスケジューラを使用します。The continuation receives caller-supplied state information and a cancellation token and uses the specified scheduler. |
ContinueWith<TResult>(Func<Task,TResult>, CancellationToken, TaskContinuationOptions, TaskScheduler) |
指定された継続のオプションに従って実行され、値を返す継続タスクを作成します。Creates a continuation that executes according to the specified continuation options and returns a value. 継続タスクは、キャンセル トークンを渡され、指定されたスケジューラを使用します。The continuation is passed a cancellation token and uses a specified scheduler. |
ContinueWith<TResult>(Func<Task,Object,TResult>, Object, TaskScheduler) |
ターゲットの Task が完了したときに非同期に実行する継続タスクを作成します。Creates a continuation that executes asynchronously when the target Task completes. この継続タスクは、呼び出し元が提供した状態情報を受け取り、指定したスケジューラを使用します。The continuation receives caller-supplied state information and uses a specified scheduler. |
ContinueWith<TResult>(Func<Task,Object,TResult>, Object, CancellationToken) |
対象の Task が完了して値を返したときに非同期的に実行される継続タスクを作成します。Creates a continuation that executes asynchronously when the target Task completes and returns a value. この継続タスクは、呼び出し元が提供した状態情報とキャンセル トークンを受け取ります。The continuation receives caller-supplied state information and a cancellation token. |
ContinueWith<TResult>(Func<Task,Object,TResult>, Object) |
呼び出し元から提供される状態情報を受け取り、対象の Task が完了したときに非同期的に実行され、値を返す継続タスクを作成します。Creates a continuation that receives caller-supplied state information and executes asynchronously when the target Task completes and returns a value. |
ContinueWith<TResult>(Func<Task,TResult>, TaskContinuationOptions) |
指定された継続のオプションに従って実行され、値を返す継続タスクを作成します。Creates a continuation that executes according to the specified continuation options and returns a value. |
ContinueWith<TResult>(Func<Task,TResult>, CancellationToken) |
対象の Task が完了して値を返したときに非同期的に実行される継続タスクを作成します。Creates a continuation that executes asynchronously when the target Task completes and returns a value. この継続タスクは、キャンセル トークンを受け取ります。The continuation receives a cancellation token. |
ContinueWith<TResult>(Func<Task,TResult>, TaskScheduler) |
対象の Task が完了して値を返したときに非同期的に実行される継続タスクを作成します。Creates a continuation that executes asynchronously when the target Task completes and returns a value. 継続タスクは、指定されたスケジューラを使用します。The continuation uses a specified scheduler. |
ContinueWith<TResult>(Func<Task,TResult>) |
対象の Task<TResult> が完了して値を返したときに非同期的に実行される継続タスクを作成します。Creates a continuation that executes asynchronously when the target Task<TResult> completes and returns a value. |
ContinueWith(Action<Task,Object>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler)
呼び出し元が提供した状態情報およびキャンセル トークンを受け取り、対象の Task の完了時に実行される継続タスクを作成します。Creates a continuation that receives caller-supplied state information and a cancellation token and that executes when the target Task completes. 継続タスクは、指定した一連の条件に基づいて実行され、指定したスケジューラが使用されます。The continuation executes based on a set of specified conditions and uses a specified scheduler.
public:
System::Threading::Tasks::Task ^ ContinueWith(Action<System::Threading::Tasks::Task ^, System::Object ^> ^ continuationAction, System::Object ^ state, System::Threading::CancellationToken cancellationToken, System::Threading::Tasks::TaskContinuationOptions continuationOptions, System::Threading::Tasks::TaskScheduler ^ scheduler);
public System.Threading.Tasks.Task ContinueWith (Action<System.Threading.Tasks.Task,object> continuationAction, object state, System.Threading.CancellationToken cancellationToken, System.Threading.Tasks.TaskContinuationOptions continuationOptions, System.Threading.Tasks.TaskScheduler scheduler);
public System.Threading.Tasks.Task ContinueWith (Action<System.Threading.Tasks.Task,object>? continuationAction, object? state, System.Threading.CancellationToken cancellationToken, System.Threading.Tasks.TaskContinuationOptions continuationOptions, System.Threading.Tasks.TaskScheduler scheduler);
member this.ContinueWith : Action<System.Threading.Tasks.Task, obj> * obj * System.Threading.CancellationToken * System.Threading.Tasks.TaskContinuationOptions * System.Threading.Tasks.TaskScheduler -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task, Object), state As Object, cancellationToken As CancellationToken, continuationOptions As TaskContinuationOptions, scheduler As TaskScheduler) As Task
パラメーター
Task の完了時に実行するアクション。An action to run when the Task completes. 実行すると、完了したタスクおよび呼び出し元が指定する状態オブジェクトがデリゲートの引数として渡されます。When run, the delegate will be passed the completed task and the caller-supplied state object as arguments.
- state
- Object
継続アクションによって使用されるデータを表すオブジェクト。An object representing data to be used by the continuation action.
- cancellationToken
- CancellationToken
新しい継続タスクに割り当てられる CancellationToken。The CancellationToken that will be assigned to the new continuation task.
- continuationOptions
- TaskContinuationOptions
継続タスクのスケジュールおよびその動作を設定するオプション。Options for when the continuation is scheduled and how it behaves. これには、OnlyOnCanceled などの基準および ExecuteSynchronously などの実行オプションが含まれます。This includes criteria, such as OnlyOnCanceled, as well as execution options, such as ExecuteSynchronously.
- scheduler
- TaskScheduler
継続タスクに関連付け、それを実行するために使用する TaskScheduler。The TaskScheduler to associate with the continuation task and to use for its execution.
戻り値
新しい継続 Task。A new continuation Task.
例外
scheduler
引数が null
です。The scheduler
argument is null
.
continuationOptions
引数が、TaskContinuationOptions の無効な値を指定しています。The continuationOptions
argument specifies an invalid value for TaskContinuationOptions.
指定された CancellationToken は既に破棄されています。The provided CancellationToken has already been disposed.
注釈
返されるは、 Task 現在のタスクが完了するまで実行されるようにスケジュールされません。The returned Task will not be scheduled for execution until the current task has completed. パラメーターで指定した条件 continuationOptions
が満たされない場合、継続タスクはスケジュールではなくキャンセルされます。If the criteria specified through the continuationOptions
parameter are not met, the continuation task will be canceled instead of scheduled.
適用対象
ContinueWith(Action<Task>, CancellationToken, TaskContinuationOptions, TaskScheduler)
対象のタスクが完了したときに、指定した TaskContinuationOptions に従って実行される継続タスクを作成します。Creates a continuation that executes when the target task competes according to the specified TaskContinuationOptions. この継続タスクは、キャンセル トークンを受け取り、指定されたスケジューラを使用します。The continuation receives a cancellation token and uses a specified scheduler.
public:
System::Threading::Tasks::Task ^ ContinueWith(Action<System::Threading::Tasks::Task ^> ^ continuationAction, System::Threading::CancellationToken cancellationToken, System::Threading::Tasks::TaskContinuationOptions continuationOptions, System::Threading::Tasks::TaskScheduler ^ scheduler);
public System.Threading.Tasks.Task ContinueWith (Action<System.Threading.Tasks.Task> continuationAction, System.Threading.CancellationToken cancellationToken, System.Threading.Tasks.TaskContinuationOptions continuationOptions, System.Threading.Tasks.TaskScheduler scheduler);
member this.ContinueWith : Action<System.Threading.Tasks.Task> * System.Threading.CancellationToken * System.Threading.Tasks.TaskContinuationOptions * System.Threading.Tasks.TaskScheduler -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task), cancellationToken As CancellationToken, continuationOptions As TaskContinuationOptions, scheduler As TaskScheduler) As Task
パラメーター
指定した continuationOptions
に従って実行するアクション。An action to run according to the specified continuationOptions
. 実行すると、完了したタスクがデリゲートの引数として渡されます。When run, the delegate will be passed the completed task as an argument.
- cancellationToken
- CancellationToken
新しい継続タスクに割り当てられる CancellationToken。The CancellationToken that will be assigned to the new continuation task.
- continuationOptions
- TaskContinuationOptions
継続タスクのスケジュールおよびその動作を設定するオプション。Options for when the continuation is scheduled and how it behaves. これには、OnlyOnCanceled などの基準および ExecuteSynchronously などの実行オプションが含まれます。This includes criteria, such as OnlyOnCanceled, as well as execution options, such as ExecuteSynchronously.
- scheduler
- TaskScheduler
継続タスクに関連付け、それを実行するために使用する TaskScheduler。The TaskScheduler to associate with the continuation task and to use for its execution.
戻り値
新しい継続 Task。A new continuation Task.
例外
トークンを作成した CancellationTokenSource は、既に破棄されています。The CancellationTokenSource that created the token has already been disposed.
continuationAction
引数が null です。The continuationAction
argument is null.
- または --or-
scheduler
引数が null です。The scheduler
argument is null.
continuationOptions
引数が、TaskContinuationOptions の無効な値を指定しています。The continuationOptions
argument specifies an invalid value for TaskContinuationOptions.
例
System.threading.tasks.task.continuewith を使用して、バックグラウンドとユーザーインターフェイススレッドの両方で作業を実行する例を次に示します。The following is an example of using ContinueWith to run work both in the background and on the user interface threads.
private void Button1_Click(object sender, EventArgs e)
{
var backgroundScheduler = TaskScheduler.Default;
var uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
Task.Factory.StartNew(delegate { DoBackgroundComputation(); },
backgroundScheduler).
ContinueWith(delegate { UpdateUI(); }, uiScheduler).
ContinueWith(delegate { DoAnotherBackgroundComputation(); },
backgroundScheduler).
ContinueWith(delegate { UpdateUIAgain(); }, uiScheduler);
}
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Dim backgroundScheduler = TaskScheduler.Default
Dim uiScheduler = TaskScheduler.FromCurrentSynchronizationContext()
Task.Factory.StartNew(Sub()
DoBackgroundComputation()
End Sub, backgroundScheduler).ContinueWith(Sub(t)
UpdateUI()
End Sub, uiScheduler).ContinueWith(Sub(t)
DoAnotherBackgroundComputation()
End Sub, backgroundScheduler).ContinueWith(Sub(t)
UpdateUIAgain()
End Sub, uiScheduler)
End Sub
注釈
返されるは、 Task 現在のタスクが完了するまで実行されるようにスケジュールされません。The returned Task will not be scheduled for execution until the current task has completed. パラメーターで指定した条件 continuationOptions
が満たされない場合、継続タスクはスケジュールではなくキャンセルされます。If the criteria specified through the continuationOptions
parameter are not met, the continuation task will be canceled instead of scheduled.
適用対象
ContinueWith(Action<Task,Object>, Object, TaskScheduler)
public:
System::Threading::Tasks::Task ^ ContinueWith(Action<System::Threading::Tasks::Task ^, System::Object ^> ^ continuationAction, System::Object ^ state, System::Threading::Tasks::TaskScheduler ^ scheduler);
public System.Threading.Tasks.Task ContinueWith (Action<System.Threading.Tasks.Task,object> continuationAction, object state, System.Threading.Tasks.TaskScheduler scheduler);
public System.Threading.Tasks.Task ContinueWith (Action<System.Threading.Tasks.Task,object>? continuationAction, object? state, System.Threading.Tasks.TaskScheduler scheduler);
member this.ContinueWith : Action<System.Threading.Tasks.Task, obj> * obj * System.Threading.Tasks.TaskScheduler -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task, Object), state As Object, scheduler As TaskScheduler) As Task
パラメーター
Task の完了時に実行するアクション。An action to run when the Task completes. 実行すると、完了したタスクおよび呼び出し元が指定する状態オブジェクトがデリゲートの引数として渡されます。When run, the delegate will be passed the completed task and the caller-supplied state object as arguments.
- state
- Object
継続アクションによって使用されるデータを表すオブジェクト。An object representing data to be used by the continuation action.
- scheduler
- TaskScheduler
継続タスクに関連付け、それを実行するために使用する TaskScheduler。The TaskScheduler to associate with the continuation task and to use for its execution.
戻り値
新しい継続 Task。A new continuation Task.
例外
scheduler
引数が null
です。The scheduler
argument is null
.
注釈
返されるは、 Task 現在のタスクが完了するまで実行されるようにスケジュールされません。実行が正常に完了したか、未処理の例外が原因でエラーが発生したか、または取り消されたため早期に終了したかのいずれかになります。The returned Task will not be scheduled for execution until the current task has completed, whether it completes due to running to completion successfully, faulting due to an unhandled exception, or exiting out early due to being canceled.
適用対象
ContinueWith(Action<Task,Object>, Object, TaskContinuationOptions)
public:
System::Threading::Tasks::Task ^ ContinueWith(Action<System::Threading::Tasks::Task ^, System::Object ^> ^ continuationAction, System::Object ^ state, System::Threading::Tasks::TaskContinuationOptions continuationOptions);
public System.Threading.Tasks.Task ContinueWith (Action<System.Threading.Tasks.Task,object> continuationAction, object state, System.Threading.Tasks.TaskContinuationOptions continuationOptions);
public System.Threading.Tasks.Task ContinueWith (Action<System.Threading.Tasks.Task,object>? continuationAction, object? state, System.Threading.Tasks.TaskContinuationOptions continuationOptions);
member this.ContinueWith : Action<System.Threading.Tasks.Task, obj> * obj * System.Threading.Tasks.TaskContinuationOptions -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task, Object), state As Object, continuationOptions As TaskContinuationOptions) As Task
パラメーター
Task の完了時に実行するアクション。An action to run when the Task completes. 実行すると、完了したタスクおよび呼び出し元が指定する状態オブジェクトがデリゲートの引数として渡されます。When run, the delegate will be passed the completed task and the caller-supplied state object as arguments.
- state
- Object
継続アクションによって使用されるデータを表すオブジェクト。An object representing data to be used by the continuation action.
- continuationOptions
- TaskContinuationOptions
継続タスクのスケジュールおよびその動作を設定するオプション。Options for when the continuation is scheduled and how it behaves. これには、OnlyOnCanceled などの基準および ExecuteSynchronously などの実行オプションが含まれます。This includes criteria, such as OnlyOnCanceled, as well as execution options, such as ExecuteSynchronously.
戻り値
新しい継続 Task。A new continuation Task.
例外
continuationAction
引数が null
です。The continuationAction
argument is null
.
continuationOptions
引数が、TaskContinuationOptions の無効な値を指定しています。The continuationOptions
argument specifies an invalid value for TaskContinuationOptions.
注釈
返されるは、 Task 現在のタスクが完了するまで実行されるようにスケジュールされません。The returned Task will not be scheduled for execution until the current task has completed. パラメーターで指定した継続条件 continuationOptions
が満たされない場合、継続タスクはスケジュールではなくキャンセルされます。If the continuation criteria specified through the continuationOptions
parameter are not met, the continuation task will be canceled instead of scheduled.
適用対象
ContinueWith(Action<Task,Object>, Object, CancellationToken)
public:
System::Threading::Tasks::Task ^ ContinueWith(Action<System::Threading::Tasks::Task ^, System::Object ^> ^ continuationAction, System::Object ^ state, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.Task ContinueWith (Action<System.Threading.Tasks.Task,object> continuationAction, object state, System.Threading.CancellationToken cancellationToken);
public System.Threading.Tasks.Task ContinueWith (Action<System.Threading.Tasks.Task,object>? continuationAction, object? state, System.Threading.CancellationToken cancellationToken);
member this.ContinueWith : Action<System.Threading.Tasks.Task, obj> * obj * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task, Object), state As Object, cancellationToken As CancellationToken) As Task
パラメーター
Task の完了時に実行するアクション。An action to run when the Task completes. 実行すると、完了したタスクおよび呼び出し元が指定する状態オブジェクトがデリゲートの引数として渡されます。When run, the delegate will be passed the completed task and the caller-supplied state object as arguments.
- state
- Object
継続アクションによって使用されるデータを表すオブジェクト。An object representing data to be used by the continuation action.
- cancellationToken
- CancellationToken
新しい継続タスクに割り当てられる CancellationToken。The CancellationToken that will be assigned to the new continuation task.
戻り値
新しい継続 Task。A new continuation Task.
例外
continuationAction
引数が null
です。The continuationAction
argument is null
.
指定された CancellationToken は既に破棄されています。The provided CancellationToken has already been disposed.
注釈
返されるは、 Task 現在のタスクが完了するまで実行されるようにスケジュールされません。実行が正常に完了したか、未処理の例外が原因でエラーが発生したか、または取り消されたため早期に終了したかのいずれかになります。The returned Task will not be scheduled for execution until the current task has completed, whether it completes due to running to completion successfully, faulting due to an unhandled exception, or exiting out early due to being canceled.
適用対象
ContinueWith(Action<Task,Object>, Object)
public:
System::Threading::Tasks::Task ^ ContinueWith(Action<System::Threading::Tasks::Task ^, System::Object ^> ^ continuationAction, System::Object ^ state);
public System.Threading.Tasks.Task ContinueWith (Action<System.Threading.Tasks.Task,object> continuationAction, object state);
public System.Threading.Tasks.Task ContinueWith (Action<System.Threading.Tasks.Task,object>? continuationAction, object? state);
member this.ContinueWith : Action<System.Threading.Tasks.Task, obj> * obj -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task, Object), state As Object) As Task
パラメーター
タスクの完了時に実行するアクション。An action to run when the task completes. 実行時に、完了したタスクと、呼び出し元が提供した状態オブジェクトが、引数としてデリゲートに渡されます。When run, the delegate is passed the completed task and a caller-supplied state object as arguments.
- state
- Object
継続アクションによって使用されるデータを表すオブジェクト。An object representing data to be used by the continuation action.
戻り値
新しい継続タスク。A new continuation task.
例外
continuationAction
引数が null
です。The continuationAction
argument is null
.
注釈
返されるは、 Task 現在のタスクが完了するまで実行されるようにスケジュールされません。実行が正常に完了したか、ハンドルされない例外が原因でエラーが発生したか、キャンセルによって早期に終了したことが原因です。The returned Task will not be scheduled for execution until the current task has completed, whether it completes due to running to completion successfully, faulting due to an unhandled exception, or exiting early due to cancellation.
適用対象
ContinueWith(Action<Task>, TaskContinuationOptions)
対象のタスクが完了したときに、指定した TaskContinuationOptions に従って実行される継続タスクを作成します。Creates a continuation that executes when the target task completes according to the specified TaskContinuationOptions.
public:
System::Threading::Tasks::Task ^ ContinueWith(Action<System::Threading::Tasks::Task ^> ^ continuationAction, System::Threading::Tasks::TaskContinuationOptions continuationOptions);
public System.Threading.Tasks.Task ContinueWith (Action<System.Threading.Tasks.Task> continuationAction, System.Threading.Tasks.TaskContinuationOptions continuationOptions);
member this.ContinueWith : Action<System.Threading.Tasks.Task> * System.Threading.Tasks.TaskContinuationOptions -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task), continuationOptions As TaskContinuationOptions) As Task
パラメーター
指定した continuationOptions
に従って実行するアクション。An action to run according to the specified continuationOptions
. 実行すると、完了したタスクがデリゲートの引数として渡されます。When run, the delegate will be passed the completed task as an argument.
- continuationOptions
- TaskContinuationOptions
継続タスクのスケジュールおよびその動作を設定するオプション。Options for when the continuation is scheduled and how it behaves. これには、OnlyOnCanceled などの基準および ExecuteSynchronously などの実行オプションが含まれます。This includes criteria, such as OnlyOnCanceled, as well as execution options, such as ExecuteSynchronously.
戻り値
新しい継続 Task。A new continuation Task.
例外
continuationAction
引数が null です。The continuationAction
argument is null.
continuationOptions
引数が、TaskContinuationOptions の無効な値を指定しています。The continuationOptions
argument specifies an invalid value for TaskContinuationOptions.
例
次の例では、を使用して、 TaskContinuationOptions 継続元タスクが完了したときに継続タスクを同期的に実行するように指定しています。The following example demonstrates using TaskContinuationOptions to specify that a continuation task should run synchronously when the antecedent task completes. (が呼び出されたときに指定されたタスクが既に完了している場合 ContinueWith 、同期継続はを呼び出しているスレッドで実行され ContinueWith ます)。(If the specified task has already completed by the time ContinueWith is called, the synchronous continuation will run on the thread calling ContinueWith.)
public class TaskCounter
{
private volatile int _count;
public void Track(Task t)
{
if (t == null) throw new ArgumentNullException("t");
Interlocked.Increment(ref _count);
t.ContinueWith(ct => Interlocked.Decrement(ref _count), TaskContinuationOptions.ExecuteSynchronously);
}
public int NumberOfActiveTasks { get { return _count; } }
}
Public Class TaskCounter
Private _count as Integer
Public Sub Track(ByVal t as Task)
If t is Nothing Then Throw New ArgumentNullException("t")
Interlocked.Increment(_count)
t.ContinueWith(Sub(ct)
Interlocked.Decrement(_count)
End Sub,
TaskContinuationOptions.ExecuteSynchronously)
End Sub
Public ReadOnly Property NumberOfActiveTasks As Integer
Get
Return _count
End Get
End Property
End Class
注釈
返されるは、 Task 現在のタスクが完了するまで実行されるようにスケジュールされません。The returned Task will not be scheduled for execution until the current task has completed. パラメーターで指定した継続条件 continuationOptions
が満たされない場合、継続タスクはスケジュールではなくキャンセルされます。If the continuation criteria specified through the continuationOptions
parameter are not met, the continuation task will be canceled instead of scheduled.
適用対象
ContinueWith(Action<Task>, CancellationToken)
public:
System::Threading::Tasks::Task ^ ContinueWith(Action<System::Threading::Tasks::Task ^> ^ continuationAction, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.Task ContinueWith (Action<System.Threading.Tasks.Task> continuationAction, System.Threading.CancellationToken cancellationToken);
member this.ContinueWith : Action<System.Threading.Tasks.Task> * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task), cancellationToken As CancellationToken) As Task
パラメーター
Task の完了時に実行するアクション。An action to run when the Task completes. 実行すると、完了したタスクがデリゲートの引数として渡されます。When run, the delegate will be passed the completed task as an argument.
- cancellationToken
- CancellationToken
新しい継続タスクに割り当てられる CancellationToken。The CancellationToken that will be assigned to the new continuation task.
戻り値
新しい継続 Task。A new continuation Task.
例外
トークンを作成した CancellationTokenSource は、既に破棄されています。The CancellationTokenSource that created the token has already been disposed.
continuationAction
引数が null です。The continuationAction
argument is null.
注釈
返されるは、 Task 現在のタスクが完了するまで実行されるようにスケジュールされません。実行が正常に完了したか、未処理の例外が原因でエラーが発生したか、または取り消されたため早期に終了したかのいずれかになります。The returned Task will not be scheduled for execution until the current task has completed, whether it completes due to running to completion successfully, faulting due to an unhandled exception, or exiting out early due to being canceled.
適用対象
ContinueWith(Action<Task>)
public:
System::Threading::Tasks::Task ^ ContinueWith(Action<System::Threading::Tasks::Task ^> ^ continuationAction);
public System.Threading.Tasks.Task ContinueWith (Action<System.Threading.Tasks.Task> continuationAction);
member this.ContinueWith : Action<System.Threading.Tasks.Task> -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task)) As Task
パラメーター
Task の完了時に実行するアクション。An action to run when the Task completes. 実行すると、完了したタスクがデリゲートの引数として渡されます。When run, the delegate will be passed the completed task as an argument.
戻り値
新しい継続 Task。A new continuation Task.
例外
continuationAction
引数が null
です。The continuationAction
argument is null
.
例
次の例では、100のランダムな日付と時刻の値を配列に設定するタスクを定義します。The following example defines a task that populates an array with 100 random date and time values. ContinueWith(Action<Task>)配列が完全に設定されたら、メソッドを使用して、最も古い日付値と最新の日付値を選択します。It uses the ContinueWith(Action<Task>) method to select the earliest and the latest date values once the array is fully populated.
using System;
using System.Threading.Tasks;
public class Example
{
public static void Main()
{
var firstTask = Task.Factory.StartNew( () => {
Random rnd = new Random();
DateTime[] dates = new DateTime[100];
Byte[] buffer = new Byte[8];
int ctr = dates.GetLowerBound(0);
while (ctr <= dates.GetUpperBound(0)) {
rnd.NextBytes(buffer);
long ticks = BitConverter.ToInt64(buffer, 0);
if (ticks <= DateTime.MinValue.Ticks | ticks >= DateTime.MaxValue.Ticks)
continue;
dates[ctr] = new DateTime(ticks);
ctr++;
}
return dates;
} );
Task continuationTask = firstTask.ContinueWith( (antecedent) => {
DateTime[] dates = antecedent.Result;
DateTime earliest = dates[0];
DateTime latest = earliest;
for (int ctr = dates.GetLowerBound(0) + 1; ctr <= dates.GetUpperBound(0); ctr++) {
if (dates[ctr] < earliest) earliest = dates[ctr];
if (dates[ctr] > latest) latest = dates[ctr];
}
Console.WriteLine("Earliest date: {0}", earliest);
Console.WriteLine("Latest date: {0}", latest);
} );
// Since a console application otherwise terminates, wait for the continuation to complete.
continuationTask.Wait();
}
}
// The example displays output like the following:
// Earliest date: 2/11/0110 12:03:41 PM
// Latest date: 7/29/9989 2:14:49 PM
Imports System.Threading.Tasks
Module Example
Public Sub Main()
Dim firstTask = Task.Factory.StartNew( Function()
Dim rnd As New Random()
Dim dates(99) As Date
Dim buffer(7) As Byte
Dim ctr As Integer = dates.GetLowerBound(0)
Do While ctr <= dates.GetUpperBound(0)
rnd.NextBytes(buffer)
Dim ticks As Long = BitConverter.ToInt64(buffer, 0)
If ticks <= DateTime.MinValue.Ticks Or ticks >= DateTime.MaxValue.Ticks Then Continue Do
dates(ctr) = New Date(ticks)
ctr += 1
Loop
Return dates
End Function )
Dim continuationTask As Task = firstTask.ContinueWith( Sub(antecedent)
Dim dates() As Date = antecedent.Result
Dim earliest As Date = dates(0)
Dim latest As Date = earliest
For ctr As Integer = dates.GetLowerBound(0) + 1 To dates.GetUpperBound(0)
If dates(ctr) < earliest Then earliest = dates(ctr)
If dates(ctr) > latest Then latest = dates(ctr)
Next
Console.WriteLine("Earliest date: {0}", earliest)
Console.WriteLine("Latest date: {0}", latest)
End Sub)
' Since a console application otherwise terminates, wait for the continuation to complete.
continuationTask.Wait()
End Sub
End Module
' The example displays output like the following:
' Earliest date: 2/11/0110 12:03:41 PM
' Latest date: 7/29/9989 2:14:49 PM
継続タスクが実行される前にコンソールアプリケーションが終了する可能性があるため、メソッドを呼び出して、この Wait() 例が終了する前に継続が実行を終了するようにします。Because a console application may terminate before the continuation task executes, the Wait() method is called to ensure that the continuation finishes executing before the example ends.
その他の例については、「 継続タスクを使用したタスクの連結」を参照してください。For an additional example, see Chaining Tasks by Using Continuation Tasks.
注釈
返されるは、 Task 現在のタスクが完了するまで実行されるようにスケジュールされません。実行が正常に完了したか、未処理の例外が原因でエラーが発生したか、または取り消されたため早期に終了したかのいずれかになります。The returned Task will not be scheduled for execution until the current task has completed, whether it completes due to running to completion successfully, faulting due to an unhandled exception, or exiting out early due to being canceled.
適用対象
ContinueWith(Action<Task>, TaskScheduler)
public:
System::Threading::Tasks::Task ^ ContinueWith(Action<System::Threading::Tasks::Task ^> ^ continuationAction, System::Threading::Tasks::TaskScheduler ^ scheduler);
public System.Threading.Tasks.Task ContinueWith (Action<System.Threading.Tasks.Task> continuationAction, System.Threading.Tasks.TaskScheduler scheduler);
member this.ContinueWith : Action<System.Threading.Tasks.Task> * System.Threading.Tasks.TaskScheduler -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task), scheduler As TaskScheduler) As Task
パラメーター
Task の完了時に実行するアクション。An action to run when the Task completes. 実行すると、完了したタスクがデリゲートの引数として渡されます。When run, the delegate will be passed the completed task as an argument.
- scheduler
- TaskScheduler
継続タスクに関連付け、それを実行するために使用する TaskScheduler。The TaskScheduler to associate with the continuation task and to use for its execution.
戻り値
新しい継続 Task。A new continuation Task.
例外
continuationAction
引数が null
です。The continuationAction
argument is null
.
- または --or-
scheduler
引数が null です。The scheduler
argument is null.
注釈
返されるは、 Task 現在のタスクが完了するまで実行されるようにスケジュールされません。実行が正常に完了したか、未処理の例外が原因でエラーが発生したか、または取り消されたため早期に終了したかのいずれかになります。The returned Task will not be scheduled for execution until the current task has completed, whether it completes due to running to completion successfully, faulting due to an unhandled exception, or exiting out early due to being canceled.
適用対象
ContinueWith<TResult>(Func<Task,Object,TResult>, Object, TaskContinuationOptions)
public:
generic <typename TResult>
System::Threading::Tasks::Task<TResult> ^ ContinueWith(Func<System::Threading::Tasks::Task ^, System::Object ^, TResult> ^ continuationFunction, System::Object ^ state, System::Threading::Tasks::TaskContinuationOptions continuationOptions);
public System.Threading.Tasks.Task<TResult> ContinueWith<TResult> (Func<System.Threading.Tasks.Task,object,TResult> continuationFunction, object state, System.Threading.Tasks.TaskContinuationOptions continuationOptions);
public System.Threading.Tasks.Task<TResult>? ContinueWith<TResult> (Func<System.Threading.Tasks.Task,object,TResult>? continuationFunction, object state, System.Threading.Tasks.TaskContinuationOptions continuationOptions);
member this.ContinueWith : Func<System.Threading.Tasks.Task, obj, 'Result> * obj * System.Threading.Tasks.TaskContinuationOptions -> System.Threading.Tasks.Task<'Result>
Public Function ContinueWith(Of TResult) (continuationFunction As Func(Of Task, Object, TResult), state As Object, continuationOptions As TaskContinuationOptions) As Task(Of TResult)
型パラメーター
- TResult
継続タスクによって生成される結果の型。The type of the result produced by the continuation.
パラメーター
Task の完了時に実行する関数。A function to run when the Task completes. 実行時に、完了したタスクおよび呼び出し元が提供した状態オブジェクトがデリゲートの引数として渡されます。When run, the delegate will be passed the completed task and the caller-supplied state object as arguments.
- state
- Object
継続関数によって使用されるデータを表すオブジェクト。An object representing data to be used by the continuation function.
- continuationOptions
- TaskContinuationOptions
継続タスクのスケジュールおよびその動作を設定するオプション。Options for when the continuation is scheduled and how it behaves. これには、OnlyOnCanceled などの基準および ExecuteSynchronously などの実行オプションが含まれます。This includes criteria, such as OnlyOnCanceled, as well as execution options, such as ExecuteSynchronously.
戻り値
新しい継続 Task<TResult>。A new continuation Task<TResult>.
例外
continuationFunction
引数が null
です。The continuationFunction
argument is null
.
continuationOptions
引数が、TaskContinuationOptions の無効な値を指定しています。The continuationOptions
argument specifies an invalid value for TaskContinuationOptions.
注釈
返されるは、 Task<TResult> 現在のタスクが完了するまで実行されるようにスケジュールされません。The returned Task<TResult> will not be scheduled for execution until the current task has completed. パラメーターで指定した継続条件 continuationOptions
が満たされない場合、継続タスクはスケジュールではなくキャンセルされます。If the continuation criteria specified through the continuationOptions
parameter are not met, the continuation task will be canceled instead of scheduled.
適用対象
ContinueWith<TResult>(Func<Task,Object,TResult>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler)
対象の Task が完了したときに、指定したタスク継続オプションに基づいて実行され、値を返す継続タスクを作成します。Creates a continuation that executes based on the specified task continuation options when the target Task completes and returns a value. この継続タスクは、呼び出し元が提供した状態情報とキャンセル トークンを受け取り、指定したスケジューラを使用します。The continuation receives caller-supplied state information and a cancellation token and uses the specified scheduler.
public:
generic <typename TResult>
System::Threading::Tasks::Task<TResult> ^ ContinueWith(Func<System::Threading::Tasks::Task ^, System::Object ^, TResult> ^ continuationFunction, System::Object ^ state, System::Threading::CancellationToken cancellationToken, System::Threading::Tasks::TaskContinuationOptions continuationOptions, System::Threading::Tasks::TaskScheduler ^ scheduler);
public System.Threading.Tasks.Task<TResult> ContinueWith<TResult> (Func<System.Threading.Tasks.Task,object,TResult> continuationFunction, object state, System.Threading.CancellationToken cancellationToken, System.Threading.Tasks.TaskContinuationOptions continuationOptions, System.Threading.Tasks.TaskScheduler scheduler);
public System.Threading.Tasks.Task<TResult> ContinueWith<TResult> (Func<System.Threading.Tasks.Task,object,TResult>? continuationFunction, object? state, System.Threading.CancellationToken cancellationToken, System.Threading.Tasks.TaskContinuationOptions continuationOptions, System.Threading.Tasks.TaskScheduler scheduler);
member this.ContinueWith : Func<System.Threading.Tasks.Task, obj, 'Result> * obj * System.Threading.CancellationToken * System.Threading.Tasks.TaskContinuationOptions * System.Threading.Tasks.TaskScheduler -> System.Threading.Tasks.Task<'Result>
Public Function ContinueWith(Of TResult) (continuationFunction As Func(Of Task, Object, TResult), state As Object, cancellationToken As CancellationToken, continuationOptions As TaskContinuationOptions, scheduler As TaskScheduler) As Task(Of TResult)
型パラメーター
- TResult
継続タスクによって生成される結果の型。The type of the result produced by the continuation.
パラメーター
Task の完了時に実行する関数。A function to run when the Task completes. 実行時に、完了したタスクおよび呼び出し元が提供した状態オブジェクトがデリゲートの引数として渡されます。When run, the delegate will be passed the completed task and the caller-supplied state object as arguments.
- state
- Object
継続関数によって使用されるデータを表すオブジェクト。An object representing data to be used by the continuation function.
- cancellationToken
- CancellationToken
新しい継続タスクに割り当てられる CancellationToken。The CancellationToken that will be assigned to the new continuation task.
- continuationOptions
- TaskContinuationOptions
継続タスクのスケジュールおよびその動作を設定するオプション。Options for when the continuation is scheduled and how it behaves. これには、OnlyOnCanceled などの基準および ExecuteSynchronously などの実行オプションが含まれます。This includes criteria, such as OnlyOnCanceled, as well as execution options, such as ExecuteSynchronously.
- scheduler
- TaskScheduler
継続タスクに関連付け、それを実行するために使用する TaskScheduler。The TaskScheduler to associate with the continuation task and to use for its execution.
戻り値
新しい継続 Task<TResult>。A new continuation Task<TResult>.
例外
scheduler
引数が null
です。The scheduler
argument is null
.
continuationOptions
引数が、TaskContinuationOptions の無効な値を指定しています。The continuationOptions
argument specifies an invalid value for TaskContinuationOptions.
指定された CancellationToken は既に破棄されています。The provided CancellationToken has already been disposed.
注釈
返されるは、 Task<TResult> 現在のタスクが完了するまで実行されるようにスケジュールされません。The returned Task<TResult> will not be scheduled for execution until the current task has completed. パラメーターで指定した条件 continuationOptions
が満たされない場合、継続タスクはスケジュールではなくキャンセルされます。If the criteria specified through the continuationOptions
parameter are not met, the continuation task will be canceled instead of scheduled.
適用対象
ContinueWith<TResult>(Func<Task,TResult>, CancellationToken, TaskContinuationOptions, TaskScheduler)
指定された継続のオプションに従って実行され、値を返す継続タスクを作成します。Creates a continuation that executes according to the specified continuation options and returns a value. 継続タスクは、キャンセル トークンを渡され、指定されたスケジューラを使用します。The continuation is passed a cancellation token and uses a specified scheduler.
public:
generic <typename TResult>
System::Threading::Tasks::Task<TResult> ^ ContinueWith(Func<System::Threading::Tasks::Task ^, TResult> ^ continuationFunction, System::Threading::CancellationToken cancellationToken, System::Threading::Tasks::TaskContinuationOptions continuationOptions, System::Threading::Tasks::TaskScheduler ^ scheduler);
public System.Threading.Tasks.Task<TResult> ContinueWith<TResult> (Func<System.Threading.Tasks.Task,TResult> continuationFunction, System.Threading.CancellationToken cancellationToken, System.Threading.Tasks.TaskContinuationOptions continuationOptions, System.Threading.Tasks.TaskScheduler scheduler);
member this.ContinueWith : Func<System.Threading.Tasks.Task, 'Result> * System.Threading.CancellationToken * System.Threading.Tasks.TaskContinuationOptions * System.Threading.Tasks.TaskScheduler -> System.Threading.Tasks.Task<'Result>
Public Function ContinueWith(Of TResult) (continuationFunction As Func(Of Task, TResult), cancellationToken As CancellationToken, continuationOptions As TaskContinuationOptions, scheduler As TaskScheduler) As Task(Of TResult)
型パラメーター
- TResult
継続タスクによって生成される結果の型。The type of the result produced by the continuation.
パラメーター
指定した continuationOptions
に従って実行する関数。A function to run according to the specified continuationOptions.
実行すると、完了したタスクがデリゲートの引数として渡されます。When run, the delegate will be passed the completed task as an argument.
- cancellationToken
- CancellationToken
新しい継続タスクに割り当てられる CancellationToken。The CancellationToken that will be assigned to the new continuation task.
- continuationOptions
- TaskContinuationOptions
継続タスクのスケジュールおよびその動作を設定するオプション。Options for when the continuation is scheduled and how it behaves. これには、OnlyOnCanceled などの基準および ExecuteSynchronously などの実行オプションが含まれます。This includes criteria, such as OnlyOnCanceled, as well as execution options, such as ExecuteSynchronously.
- scheduler
- TaskScheduler
継続タスクに関連付け、それを実行するために使用する TaskScheduler。The TaskScheduler to associate with the continuation task and to use for its execution.
戻り値
新しい継続 Task<TResult>。A new continuation Task<TResult>.
例外
Task は破棄されています。The Task has been disposed.
- または --or- トークンを作成した CancellationTokenSource は、既に破棄されています。The CancellationTokenSource that created the token has already been disposed.
continuationFunction
引数が null です。The continuationFunction
argument is null.
- または --or-
scheduler
引数が null です。The scheduler
argument is null.
continuationOptions
引数が、TaskContinuationOptions の無効な値を指定しています。The continuationOptions
argument specifies an invalid value for TaskContinuationOptions.
例
次の例は、System.threading.tasks.task.continuewith メソッドを継続オプションと共に使用する方法を示しています。The following example shows how to use the ContinueWith method with continuation options:
using System;
using System.Threading;
using System.Threading.Tasks;
class ContinuationOptionsDemo
{
// Demonstrated features:
// TaskContinuationOptions
// Task.ContinueWith()
// Task.Factory
// Task.Wait()
// Expected results:
// This sample demonstrates branched continuation sequences - Task+Commit or Task+Rollback.
// Notice that no if statements are used.
// The first sequence is successful - tran1 and commitTran1 are executed. rollbackTran1 is canceled.
// The second sequence is unsuccessful - tran2 and rollbackTran2 are executed. tran2 is faulted, and commitTran2 is canceled.
// Documentation:
// http://msdn.microsoft.com/library/system.threading.tasks.taskcontinuationoptions(VS.100).aspx
static void Main()
{
Action success = () => Console.WriteLine("Task={0}, Thread={1}: Begin successful transaction",
Task.CurrentId, Thread.CurrentThread.ManagedThreadId);
Action failure = () =>
{
Console.WriteLine("Task={0}, Thread={1}: Begin transaction and encounter an error",
Task.CurrentId, Thread.CurrentThread.ManagedThreadId);
throw new InvalidOperationException("SIMULATED EXCEPTION");
};
Action<Task> commit = (antecendent) => Console.WriteLine("Task={0}, Thread={1}: Commit transaction",
Task.CurrentId, Thread.CurrentThread.ManagedThreadId);
Action<Task> rollback = (antecendent) =>
{
// "Observe" your antecedent's exception so as to avoid an exception
// being thrown on the finalizer thread
var unused = antecendent.Exception;
Console.WriteLine("Task={0}, Thread={1}: Rollback transaction",
Task.CurrentId, Thread.CurrentThread.ManagedThreadId);
};
// Successful transaction - Begin + Commit
Console.WriteLine("Demonstrating a successful transaction");
// Initial task
// Treated as "fire-and-forget" -- any exceptions will be cleaned up in rollback continuation
Task tran1 = Task.Factory.StartNew(success);
// The following task gets scheduled only if tran1 completes successfully
var commitTran1 = tran1.ContinueWith(commit, TaskContinuationOptions.OnlyOnRanToCompletion);
// The following task gets scheduled only if tran1 DOES NOT complete successfully
var rollbackTran1 = tran1.ContinueWith(rollback, TaskContinuationOptions.NotOnRanToCompletion);
// For demo purposes, wait for the sample to complete
commitTran1.Wait();
// -----------------------------------------------------------------------------------
// Failed transaction - Begin + exception + Rollback
Console.WriteLine("\nDemonstrating a failed transaction");
// Initial task
// Treated as "fire-and-forget" -- any exceptions will be cleaned up in rollback continuation
Task tran2 = Task.Factory.StartNew(failure);
// The following task gets scheduled only if tran2 completes successfully
var commitTran2 = tran2.ContinueWith(commit, TaskContinuationOptions.OnlyOnRanToCompletion);
// The following task gets scheduled only if tran2 DOES NOT complete successfully
var rollbackTran2 = tran2.ContinueWith(rollback, TaskContinuationOptions.NotOnRanToCompletion);
// For demo purposes, wait for the sample to complete
rollbackTran2.Wait();
}
}
Imports System.Threading
Imports System.Threading.Tasks
Module ContuationOptionsDemo
' Demonstrated features:
' TaskContinuationOptions
' Task.ContinueWith()
' Task.Factory
' Task.Wait()
' Expected results:
' This sample demonstrates branched continuation sequences - Task+Commit or Task+Rollback.
' Notice that no if statements are used.
' The first sequence is successful - tran1 and commitTran1 are executed. rollbackTran1 is canceled.
' The second sequence is unsuccessful - tran2 and rollbackTran2 are executed. tran2 is faulted, and commitTran2 is canceled.
' Documentation:
' http://msdn.microsoft.com/library/system.threading.tasks.taskcontinuationoptions(VS.100).aspx
Private Sub Main()
Dim success As Action = Sub()
Console.WriteLine("Task={0}, Thread={1}: Begin successful transaction", Task.CurrentId, Thread.CurrentThread.ManagedThreadId)
End Sub
Dim failure As Action = Sub()
Console.WriteLine("Task={0}, Thread={1}: Begin transaction and encounter an error", Task.CurrentId, Thread.CurrentThread.ManagedThreadId)
Throw New InvalidOperationException("SIMULATED EXCEPTION")
End Sub
Dim commit As Action(Of Task) = Sub(antecendent)
Console.WriteLine("Task={0}, Thread={1}: Commit transaction", Task.CurrentId, Thread.CurrentThread.ManagedThreadId)
End Sub
Dim rollback As Action(Of Task) = Sub(antecendent)
' "Observe" your antecedent's exception so as to avoid an exception
' being thrown on the finalizer thread
Dim unused = antecendent.Exception
Console.WriteLine("Task={0}, Thread={1}: Rollback transaction", Task.CurrentId, Thread.CurrentThread.ManagedThreadId)
End Sub
' Successful transaction - Begin + Commit
Console.WriteLine("Demonstrating a successful transaction")
' Initial task
' Treated as "fire-and-forget" -- any exceptions will be cleaned up in rollback continuation
Dim tran1 As Task = Task.Factory.StartNew(success)
' The following task gets scheduled only if tran1 completes successfully
Dim commitTran1 = tran1.ContinueWith(commit, TaskContinuationOptions.OnlyOnRanToCompletion)
' The following task gets scheduled only if tran1 DOES NOT complete successfully
Dim rollbackTran1 = tran1.ContinueWith(rollback, TaskContinuationOptions.NotOnRanToCompletion)
' For demo purposes, wait for the sample to complete
commitTran1.Wait()
' -----------------------------------------------------------------------------------
' Failed transaction - Begin + exception + Rollback
Console.WriteLine(vbLf & "Demonstrating a failed transaction")
' Initial task
' Treated as "fire-and-forget" -- any exceptions will be cleaned up in rollback continuation
Dim tran2 As Task = Task.Factory.StartNew(failure)
' The following task gets scheduled only if tran2 completes successfully
Dim commitTran2 = tran2.ContinueWith(commit, TaskContinuationOptions.OnlyOnRanToCompletion)
' The following task gets scheduled only if tran2 DOES NOT complete successfully
Dim rollbackTran2 = tran2.ContinueWith(rollback, TaskContinuationOptions.NotOnRanToCompletion)
' For demo purposes, wait for the sample to complete
rollbackTran2.Wait()
End Sub
End Module
注釈
返されるは、 Task<TResult> 現在のタスクが完了するまで実行されるようにスケジュールされません。The returned Task<TResult> will not be scheduled for execution until the current task has completed. パラメーターで指定した条件 continuationOptions
が満たされない場合、継続タスクはスケジュールではなくキャンセルされます。If the criteria specified through the continuationOptions
parameter are not met, the continuation task will be canceled instead of scheduled.
適用対象
ContinueWith<TResult>(Func<Task,Object,TResult>, Object, TaskScheduler)
public:
generic <typename TResult>
System::Threading::Tasks::Task<TResult> ^ ContinueWith(Func<System::Threading::Tasks::Task ^, System::Object ^, TResult> ^ continuationFunction, System::Object ^ state, System::Threading::Tasks::TaskScheduler ^ scheduler);
public System.Threading.Tasks.Task<TResult> ContinueWith<TResult> (Func<System.Threading.Tasks.Task,object,TResult> continuationFunction, object state, System.Threading.Tasks.TaskScheduler scheduler);
public System.Threading.Tasks.Task<TResult> ContinueWith<TResult> (Func<System.Threading.Tasks.Task,object,TResult>? continuationFunction, object? state, System.Threading.Tasks.TaskScheduler scheduler);
member this.ContinueWith : Func<System.Threading.Tasks.Task, obj, 'Result> * obj * System.Threading.Tasks.TaskScheduler -> System.Threading.Tasks.Task<'Result>
Public Function ContinueWith(Of TResult) (continuationFunction As Func(Of Task, Object, TResult), state As Object, scheduler As TaskScheduler) As Task(Of TResult)
型パラメーター
- TResult
継続タスクによって生成される結果の型。The type of the result produced by the continuation.
パラメーター
Task の完了時に実行する関数。A function to run when the Task completes. 実行時に、完了したタスクおよび呼び出し元が提供した状態オブジェクトがデリゲートの引数として渡されます。When run, the delegate will be passed the completed task and the caller-supplied state object as arguments.
- state
- Object
継続関数によって使用されるデータを表すオブジェクト。An object representing data to be used by the continuation function.
- scheduler
- TaskScheduler
継続タスクに関連付け、それを実行するために使用する TaskScheduler。The TaskScheduler to associate with the continuation task and to use for its execution.
戻り値
新しい継続 Task<TResult>。A new continuation Task<TResult>.
例外
scheduler
引数が null
です。The scheduler
argument is null
.
注釈
返されるは、 Task<TResult> 現在のタスクが完了するまで実行されるようにスケジュールされません。実行が正常に完了したか、未処理の例外が原因でエラーが発生したか、または取り消されたため早期に終了したかのいずれかになります。The returned Task<TResult> will not be scheduled for execution until the current task has completed, whether it completes due to running to completion successfully, faulting due to an unhandled exception, or exiting out early due to being canceled.
適用対象
ContinueWith<TResult>(Func<Task,Object,TResult>, Object, CancellationToken)
public:
generic <typename TResult>
System::Threading::Tasks::Task<TResult> ^ ContinueWith(Func<System::Threading::Tasks::Task ^, System::Object ^, TResult> ^ continuationFunction, System::Object ^ state, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.Task<TResult> ContinueWith<TResult> (Func<System.Threading.Tasks.Task,object,TResult> continuationFunction, object state, System.Threading.CancellationToken cancellationToken);
public System.Threading.Tasks.Task<TResult>? ContinueWith<TResult> (Func<System.Threading.Tasks.Task,object,TResult>? continuationFunction, object state, System.Threading.CancellationToken cancellationToken);
member this.ContinueWith : Func<System.Threading.Tasks.Task, obj, 'Result> * obj * System.Threading.CancellationToken -> System.Threading.Tasks.Task<'Result>
Public Function ContinueWith(Of TResult) (continuationFunction As Func(Of Task, Object, TResult), state As Object, cancellationToken As CancellationToken) As Task(Of TResult)
型パラメーター
- TResult
継続タスクによって生成される結果の型。The type of the result produced by the continuation.
パラメーター
Task の完了時に実行する関数。A function to run when the Task completes. 実行時に、完了したタスクおよび呼び出し元が提供した状態オブジェクトがデリゲートの引数として渡されます。When run, the delegate will be passed the completed task and the caller-supplied state object as arguments.
- state
- Object
継続関数によって使用されるデータを表すオブジェクト。An object representing data to be used by the continuation function.
- cancellationToken
- CancellationToken
新しい継続タスクに割り当てられる CancellationToken。The CancellationToken that will be assigned to the new continuation task.
戻り値
新しい継続 Task<TResult>。A new continuation Task<TResult>.
例外
continuationFunction
引数が null
です。The continuationFunction
argument is null
.
指定された CancellationToken は既に破棄されています。The provided CancellationToken has already been disposed.
注釈
返されるは、 Task<TResult> 現在のタスクが完了するまで実行されるようにスケジュールされません。実行が正常に完了したか、未処理の例外が原因でエラーが発生したか、または取り消されたため早期に終了したかのいずれかになります。The returned Task<TResult> will not be scheduled for execution until the current task has completed, whether it completes due to running to completion successfully, faulting due to an unhandled exception, or exiting out early due to being canceled.
適用対象
ContinueWith<TResult>(Func<Task,Object,TResult>, Object)
public:
generic <typename TResult>
System::Threading::Tasks::Task<TResult> ^ ContinueWith(Func<System::Threading::Tasks::Task ^, System::Object ^, TResult> ^ continuationFunction, System::Object ^ state);
public System.Threading.Tasks.Task<TResult> ContinueWith<TResult> (Func<System.Threading.Tasks.Task,object,TResult> continuationFunction, object state);
public System.Threading.Tasks.Task<TResult>? ContinueWith<TResult> (Func<System.Threading.Tasks.Task,object,TResult>? continuationFunction, object state);
member this.ContinueWith : Func<System.Threading.Tasks.Task, obj, 'Result> * obj -> System.Threading.Tasks.Task<'Result>
Public Function ContinueWith(Of TResult) (continuationFunction As Func(Of Task, Object, TResult), state As Object) As Task(Of TResult)
型パラメーター
- TResult
継続タスクによって生成される結果の型。The type of the result produced by the continuation.
パラメーター
Task の完了時に実行する関数。A function to run when the Task completes. 実行すると、完了したタスクおよび呼び出し元が指定する状態オブジェクトがデリゲートの引数として渡されます。When run, the delegate will be passed the completed task and the caller-supplied state object as arguments.
- state
- Object
継続関数によって使用されるデータを表すオブジェクト。An object representing data to be used by the continuation function.
戻り値
新しい継続 Task<TResult>。A new continuation Task<TResult>.
例外
continuationFunction
引数が null
です。The continuationFunction
argument is null
.
注釈
返されるは、 Task<TResult> 現在のタスクが完了するまで実行されるようにスケジュールされません。実行が正常に完了したか、未処理の例外が原因でエラーが発生したか、または取り消されたため早期に終了したかのいずれかになります。The returned Task<TResult> will not be scheduled for execution until the current task has completed, whether it completes due to running to completion successfully, faulting due to an unhandled exception, or exiting out early due to being canceled.
適用対象
ContinueWith<TResult>(Func<Task,TResult>, TaskContinuationOptions)
指定された継続のオプションに従って実行され、値を返す継続タスクを作成します。Creates a continuation that executes according to the specified continuation options and returns a value.
public:
generic <typename TResult>
System::Threading::Tasks::Task<TResult> ^ ContinueWith(Func<System::Threading::Tasks::Task ^, TResult> ^ continuationFunction, System::Threading::Tasks::TaskContinuationOptions continuationOptions);
public System.Threading.Tasks.Task<TResult> ContinueWith<TResult> (Func<System.Threading.Tasks.Task,TResult> continuationFunction, System.Threading.Tasks.TaskContinuationOptions continuationOptions);
member this.ContinueWith : Func<System.Threading.Tasks.Task, 'Result> * System.Threading.Tasks.TaskContinuationOptions -> System.Threading.Tasks.Task<'Result>
Public Function ContinueWith(Of TResult) (continuationFunction As Func(Of Task, TResult), continuationOptions As TaskContinuationOptions) As Task(Of TResult)
型パラメーター
- TResult
継続タスクによって生成される結果の型。The type of the result produced by the continuation.
パラメーター
continuationOptions
で指定した条件に従って実行する関数。A function to run according to the condition specified in continuationOptions
. 実行すると、完了したタスクがデリゲートの引数として渡されます。When run, the delegate will be passed the completed task as an argument.
- continuationOptions
- TaskContinuationOptions
継続タスクのスケジュールおよびその動作を設定するオプション。Options for when the continuation is scheduled and how it behaves. これには、OnlyOnCanceled などの基準および ExecuteSynchronously などの実行オプションが含まれます。This includes criteria, such as OnlyOnCanceled, as well as execution options, such as ExecuteSynchronously.
戻り値
新しい継続 Task<TResult>。A new continuation Task<TResult>.
例外
continuationFunction
引数が null です。The continuationFunction
argument is null.
continuationOptions
引数が、TaskContinuationOptions の無効な値を指定しています。The continuationOptions
argument specifies an invalid value for TaskContinuationOptions.
注釈
返されるは、 Task<TResult> 現在のタスクが完了するまで実行されるようにスケジュールされません。The returned Task<TResult> will not be scheduled for execution until the current task has completed. パラメーターで指定した継続条件 continuationOptions
が満たされない場合、継続タスクはスケジュールではなくキャンセルされます。If the continuation criteria specified through the continuationOptions
parameter are not met, the continuation task will be canceled instead of scheduled.
適用対象
ContinueWith<TResult>(Func<Task,TResult>, CancellationToken)
public:
generic <typename TResult>
System::Threading::Tasks::Task<TResult> ^ ContinueWith(Func<System::Threading::Tasks::Task ^, TResult> ^ continuationFunction, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.Task<TResult> ContinueWith<TResult> (Func<System.Threading.Tasks.Task,TResult> continuationFunction, System.Threading.CancellationToken cancellationToken);
member this.ContinueWith : Func<System.Threading.Tasks.Task, 'Result> * System.Threading.CancellationToken -> System.Threading.Tasks.Task<'Result>
Public Function ContinueWith(Of TResult) (continuationFunction As Func(Of Task, TResult), cancellationToken As CancellationToken) As Task(Of TResult)
型パラメーター
- TResult
継続タスクによって生成される結果の型。The type of the result produced by the continuation.
パラメーター
Task の完了時に実行する関数。A function to run when the Task completes. 実行すると、完了したタスクがデリゲートの引数として渡されます。When run, the delegate will be passed the completed task as an argument.
- cancellationToken
- CancellationToken
新しい継続タスクに割り当てられる CancellationToken。The CancellationToken that will be assigned to the new continuation task.
戻り値
新しい継続 Task<TResult>。A new continuation Task<TResult>.
例外
Task は破棄されています。The Task has been disposed.
- または --or- トークンを作成した CancellationTokenSource は、既に破棄されています。The CancellationTokenSource that created the token has already been disposed.
continuationFunction
引数が null です。The continuationFunction
argument is null.
注釈
返されるは、 Task<TResult> 現在のタスクが完了するまで実行されるようにスケジュールされません。実行が正常に完了したか、未処理の例外が原因でエラーが発生したか、または取り消されたため早期に終了したかのいずれかになります。The returned Task<TResult> will not be scheduled for execution until the current task has completed, whether it completes due to running to completion successfully, faulting due to an unhandled exception, or exiting out early due to being canceled.
適用対象
ContinueWith<TResult>(Func<Task,TResult>, TaskScheduler)
public:
generic <typename TResult>
System::Threading::Tasks::Task<TResult> ^ ContinueWith(Func<System::Threading::Tasks::Task ^, TResult> ^ continuationFunction, System::Threading::Tasks::TaskScheduler ^ scheduler);
public System.Threading.Tasks.Task<TResult> ContinueWith<TResult> (Func<System.Threading.Tasks.Task,TResult> continuationFunction, System.Threading.Tasks.TaskScheduler scheduler);
member this.ContinueWith : Func<System.Threading.Tasks.Task, 'Result> * System.Threading.Tasks.TaskScheduler -> System.Threading.Tasks.Task<'Result>
Public Function ContinueWith(Of TResult) (continuationFunction As Func(Of Task, TResult), scheduler As TaskScheduler) As Task(Of TResult)
型パラメーター
- TResult
継続タスクによって生成される結果の型。The type of the result produced by the continuation.
パラメーター
Task の完了時に実行する関数。A function to run when the Task completes. 実行すると、完了したタスクがデリゲートの引数として渡されます。When run, the delegate will be passed the completed task as an argument.
- scheduler
- TaskScheduler
継続タスクに関連付け、それを実行するために使用する TaskScheduler。The TaskScheduler to associate with the continuation task and to use for its execution.
戻り値
新しい継続 Task<TResult>。A new continuation Task<TResult>.
例外
continuationFunction
引数が null です。The continuationFunction
argument is null.
- または --or-
scheduler
引数が null です。The scheduler
argument is null.
注釈
返されるは、 Task<TResult> 現在のタスクが完了するまで実行されるようにスケジュールされません。実行が正常に完了したか、未処理の例外が原因でエラーが発生したか、または取り消されたため早期に終了したかのいずれかになります。The returned Task<TResult> will not be scheduled for execution until the current task has completed, whether it completes due to running to completion successfully, faulting due to an unhandled exception, or exiting out early due to being canceled.
適用対象
ContinueWith<TResult>(Func<Task,TResult>)
対象の Task<TResult> が完了して値を返したときに非同期的に実行される継続タスクを作成します。Creates a continuation that executes asynchronously when the target Task<TResult> completes and returns a value.
public:
generic <typename TResult>
System::Threading::Tasks::Task<TResult> ^ ContinueWith(Func<System::Threading::Tasks::Task ^, TResult> ^ continuationFunction);
public System.Threading.Tasks.Task<TResult> ContinueWith<TResult> (Func<System.Threading.Tasks.Task,TResult> continuationFunction);
member this.ContinueWith : Func<System.Threading.Tasks.Task, 'Result> -> System.Threading.Tasks.Task<'Result>
Public Function ContinueWith(Of TResult) (continuationFunction As Func(Of Task, TResult)) As Task(Of TResult)
型パラメーター
- TResult
継続タスクによって生成される結果の型。The type of the result produced by the continuation.
パラメーター
Task<TResult> の完了時に実行する関数。A function to run when the Task<TResult> completes. 実行すると、完了したタスクがデリゲートの引数として渡されます。When run, the delegate will be passed the completed task as an argument.
戻り値
新しい継続タスク。A new continuation task.
例外
continuationFunction
引数が null です。The continuationFunction
argument is null.
例
次の例は、System.threading.tasks.task.continuewith メソッドの使用方法を示しています。The following example shows how to use the ContinueWith method:
using System;
using System.Threading;
using System.Threading.Tasks;
class ContinuationSimpleDemo
{
// Demonstrated features:
// Task.Factory
// Task.ContinueWith()
// Task.Wait()
// Expected results:
// A sequence of three unrelated tasks is created and executed in this order - alpha, beta, gamma.
// A sequence of three related tasks is created - each task negates its argument and passes is to the next task: 5, -5, 5 is printed.
// A sequence of three unrelated tasks is created where tasks have different types.
// Documentation:
// http://msdn.microsoft.com/library/system.threading.tasks.taskfactory_members(VS.100).aspx
static void Main()
{
Action<string> action =
(str) =>
Console.WriteLine("Task={0}, str={1}, Thread={2}", Task.CurrentId, str, Thread.CurrentThread.ManagedThreadId);
// Creating a sequence of action tasks (that return no result).
Console.WriteLine("Creating a sequence of action tasks (that return no result)");
Task.Factory.StartNew(() => action("alpha"))
.ContinueWith(antecendent => action("beta")) // Antecedent data is ignored
.ContinueWith(antecendent => action("gamma"))
.Wait();
Func<int, int> negate =
(n) =>
{
Console.WriteLine("Task={0}, n={1}, -n={2}, Thread={3}", Task.CurrentId, n, -n, Thread.CurrentThread.ManagedThreadId);
return -n;
};
// Creating a sequence of function tasks where each continuation uses the result from its antecendent
Console.WriteLine("\nCreating a sequence of function tasks where each continuation uses the result from its antecendent");
Task<int>.Factory.StartNew(() => negate(5))
.ContinueWith(antecendent => negate(antecendent.Result)) // Antecedent result feeds into continuation
.ContinueWith(antecendent => negate(antecendent.Result))
.Wait();
// Creating a sequence of tasks where you can mix and match the types
Console.WriteLine("\nCreating a sequence of tasks where you can mix and match the types");
Task<int>.Factory.StartNew(() => negate(6))
.ContinueWith(antecendent => action("x"))
.ContinueWith(antecendent => negate(7))
.Wait();
}
}
Imports System.Threading
Imports System.Threading.Tasks
Module ContinuationDemo
' Demonstrated features:
' Task.Factory
' Task.ContinueWith()
' Task.Wait()
' Expected results:
' A sequence of three unrelated tasks is created and executed in this order - alpha, beta, gamma.
' A sequence of three related tasks is created - each task negates its argument and passes is to the next task: 5, -5, 5 is printed.
' A sequence of three unrelated tasks is created where tasks have different types.
' Documentation:
' http://msdn.microsoft.com/library/system.threading.tasks.taskfactory_members(VS.100).aspx
Sub Main()
Dim action As Action(Of String) = Sub(str) Console.WriteLine("Task={0}, str={1}, Thread={2}", Task.CurrentId, str, Thread.CurrentThread.ManagedThreadId)
' Creating a sequence of action tasks (that return no result).
Console.WriteLine("Creating a sequence of action tasks (that return no result)")
' Continuations ignore antecedent data
Task.Factory.StartNew(Sub() action("alpha")).ContinueWith(Sub(antecendent) action("beta")).ContinueWith(Sub(antecendent) action("gamma")).Wait()
Dim negate As Func(Of Integer, Integer) = Function(n)
Console.WriteLine("Task={0}, n={1}, -n={2}, Thread={3}", Task.CurrentId, n, -n, Thread.CurrentThread.ManagedThreadId)
Return -n
End Function
' Creating a sequence of function tasks where each continuation uses the result from its antecendent
Console.WriteLine(vbLf & "Creating a sequence of function tasks where each continuation uses the result from its antecendent")
Task(Of Integer).Factory.StartNew(Function() negate(5)).ContinueWith(Function(antecendent) negate(antecendent.Result)).ContinueWith(Function(antecendent) negate(antecendent.Result)).Wait()
' Creating a sequence of tasks where you can mix and match the types
Console.WriteLine(vbLf & "Creating a sequence of tasks where you can mix and match the types")
Task(Of Integer).Factory.StartNew(Function() negate(6)).ContinueWith(Sub(antecendent) action("x")).ContinueWith(Function(antecendent) negate(7)).Wait()
End Sub
End Module
注釈
返されるは、 Task<TResult> 現在のタスクが完了するまで実行されるようにスケジュールされません。実行が正常に完了したか、未処理の例外が原因でエラーが発生したか、または取り消されたため早期に終了したかのいずれかになります。The returned Task<TResult> will not be scheduled for execution until the current task has completed, whether it completes due to running to completion successfully, faulting due to an unhandled exception, or exiting out early due to being canceled.