diff --git a/Softeq.XToolkit.Common/Extensions/TaskExtensions.cs b/Softeq.XToolkit.Common/Extensions/TaskExtensions.cs index aae472c4..7a972575 100644 --- a/Softeq.XToolkit.Common/Extensions/TaskExtensions.cs +++ b/Softeq.XToolkit.Common/Extensions/TaskExtensions.cs @@ -166,6 +166,27 @@ public static Task WithCancellation(this Task task, CancellationToken c TaskScheduler.Default); } + /// + /// Simple wrapper for execution task with . + /// Target Task will continue running, but the execution will be returned. + /// + /// The task to cancellation for. + /// + /// The that will be assigned to the new continuation task. + /// + /// The task was canceled. + /// Task result. + public static Task WithCancellation(this Task task, CancellationToken cancellationToken) + { + return task.IsCompleted + ? task + : task.ContinueWith( + completedTask => completedTask.GetAwaiter().GetResult(), + cancellationToken, + TaskContinuationOptions.ExecuteSynchronously, + TaskScheduler.Default); + } + /// /// Useful for fire-and-forget calls to async methods. /// Exceptions will be ignored.