Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Softeq.XToolkit.Common/Extensions/TaskExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,27 @@ public static Task<T> WithCancellation<T>(this Task<T> task, CancellationToken c
TaskScheduler.Default);
}

/// <summary>
/// Simple wrapper for execution task with <see cref="T:System.Threading.CancellationToken"/>.
/// Target Task will continue running, but the execution will be returned.
/// </summary>
/// <param name="task">The task to cancellation for.</param>
/// <param name="cancellationToken">
/// The <see cref="T:System.Threading.CancellationToken"/> that will be assigned to the new continuation task.
/// </param>
/// <exception cref="T:System.Threading.Tasks.TaskCanceledException">The task was canceled.</exception>
/// <returns>Task result.</returns>
public static Task WithCancellation(this Task task, CancellationToken cancellationToken)
{
return task.IsCompleted
? task
: task.ContinueWith(
completedTask => completedTask.GetAwaiter().GetResult(),
cancellationToken,
TaskContinuationOptions.ExecuteSynchronously,
TaskScheduler.Default);
}

/// <summary>
/// Useful for fire-and-forget calls to async methods.
/// Exceptions will be ignored.
Expand Down