-
Notifications
You must be signed in to change notification settings - Fork 5k
/
TaskCompletionSourceExtensions.cs
55 lines (51 loc) · 3.33 KB
/
TaskCompletionSourceExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
//
namespace System.Threading.Tasks
{
/// <summary>Extension methods for TaskCompletionSource.</summary>
public static class TaskCompletionSourceExtensions
{
/// <summary>Transfers the result of a Task to the TaskCompletionSource.</summary>
/// <typeparam name="TResult">Specifies the type of the result.</typeparam>
/// <param name="resultSetter">The TaskCompletionSource.</param>
/// <param name="task">The task whose completion results should be transfered.</param>
public static void SetFromTask<TResult>(this TaskCompletionSource<TResult> resultSetter, Task task)
{
switch (task.Status)
{
case TaskStatus.RanToCompletion: resultSetter.SetResult(task is Task<TResult> ? ((Task<TResult>)task).Result : default); break;
case TaskStatus.Faulted: resultSetter.SetException(task.Exception.InnerExceptions); break;
case TaskStatus.Canceled: resultSetter.SetCanceled(); break;
default: throw new InvalidOperationException("The task was not completed.");
}
}
/// <summary>Transfers the result of a Task to the TaskCompletionSource.</summary>
/// <typeparam name="TResult">Specifies the type of the result.</typeparam>
/// <param name="resultSetter">The TaskCompletionSource.</param>
/// <param name="task">The task whose completion results should be transfered.</param>
public static void SetFromTask<TResult>(this TaskCompletionSource<TResult> resultSetter, Task<TResult> task) =>
SetFromTask(resultSetter, (Task)task);
/// <summary>Attempts to transfer the result of a Task to the TaskCompletionSource.</summary>
/// <typeparam name="TResult">Specifies the type of the result.</typeparam>
/// <param name="resultSetter">The TaskCompletionSource.</param>
/// <param name="task">The task whose completion results should be transfered.</param>
/// <returns>Whether the transfer could be completed.</returns>
public static bool TrySetFromTask<TResult>(this TaskCompletionSource<TResult> resultSetter, Task task) =>
task.Status switch
{
TaskStatus.RanToCompletion => resultSetter.TrySetResult(task is Task<TResult> ? ((Task<TResult>)task).Result : default),
TaskStatus.Faulted => resultSetter.TrySetException(task.Exception.InnerExceptions),
TaskStatus.Canceled => resultSetter.TrySetCanceled(),
_ => throw new InvalidOperationException("The task was not completed."),
};
/// <summary>Attempts to transfer the result of a Task to the TaskCompletionSource.</summary>
/// <typeparam name="TResult">Specifies the type of the result.</typeparam>
/// <param name="resultSetter">The TaskCompletionSource.</param>
/// <param name="task">The task whose completion results should be transfered.</param>
/// <returns>Whether the transfer could be completed.</returns>
public static bool TrySetFromTask<TResult>(this TaskCompletionSource<TResult> resultSetter, Task<TResult> task) =>
TrySetFromTask(resultSetter, (Task)task);
}
}