/
ObviousExtensions.cs
99 lines (89 loc) 路 5.42 KB
/
ObviousExtensions.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
锘縰sing System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace ObviousAwait
{
/// <summary>
/// Provides access to the extension methods KeepContext() and FreeContext() to alias ConfigureAwait(true/false).
/// </summary>
public static class ObviousExtensions
{
/// <summary>
/// Configures an awaiter to await a given task and captures the current context to make sure that
/// subsequent code runs on the same context, like the UI thread of example.
/// You may want to use this if your async code is coupled tightly to a user interface.
/// Equals to .ConfigureAwait(true).
/// </summary>
/// <param name="t">The task to configure an awaiter for.</param>
/// <returns>An object used to await this task.</returns>
public static ConfiguredTaskAwaitable KeepContext(this Task t) => t.ConfigureAwait(true);
/// <summary>
/// Configures an awaiter to await a given task and captures the current context to make sure that
/// subsequent code runs on the same context, like the UI thread of example.
/// You may want to use this if your async code is coupled tightly to a user interface.
/// Equals to .ConfigureAwait(true).
/// </summary>
/// <typeparam name="T">The type of the result of the task to wait for.</typeparam>
/// <param name="t">The task to configure an awaiter for.</param>
/// <returns>An object used to await this task.</returns>
public static ConfiguredTaskAwaitable<T> KeepContext<T>(this Task<T> t) => t.ConfigureAwait(true);
/// <summary>
/// Configures an awaiter to await a given task without capturing a context. This means that
/// subsequent code is likely to run on another context than the one the awaited task was called from.
/// You should use this method on almost every awaited method. But pay attention if your async code is coupled tightly to a user interface.
/// Equals to .ConfigureAwait(false).
/// </summary>
/// <param name="t">The task to configure an awaiter for.</param>
/// <returns>An object used to await this task.</returns>
public static ConfiguredTaskAwaitable FreeContext(this Task t) => t.ConfigureAwait(false);
/// <summary>
/// Configures an awaiter to await a given task without capturing a context. This means that
/// subsequent code is likely to run on another context than the one the awaited task was called from.
/// You should use this method on almost every awaited method. But pay attention if your async code is coupled tightly to a user interface.
/// Equals to .ConfigureAwait(false).
/// </summary>
/// <typeparam name="T">The type of the result of the task to wait for.</typeparam>
/// <param name="t">The task to configure an awaiter for.</param>
/// <returns>An object used to await this task.</returns>
public static ConfiguredTaskAwaitable<T> FreeContext<T>(this Task<T> t) => t.ConfigureAwait(false);
#if NETSTANDARD2_1_OR_GREATER
/// <summary>
/// Configures an awaiter to await a given task and captures the current context to make sure that
/// subsequent code runs on the same context, like the UI thread of example.
/// You may want to use this if your async code is coupled tightly to a user interface.
/// Equals to .ConfigureAwait(true).
/// </summary>
/// <param name="t">The task to configure an awaiter for.</param>
/// <returns>An object used to await this task.</returns>
public static ConfiguredValueTaskAwaitable KeepContext(this ValueTask t) => t.ConfigureAwait(true);
/// <summary>
/// Configures an awaiter to await a given task and captures the current context to make sure that
/// subsequent code runs on the same context, like the UI thread of example.
/// You may want to use this if your async code is coupled tightly to a user interface.
/// Equals to .ConfigureAwait(true).
/// </summary>
/// <typeparam name="T">The type of the result of the task to wait for.</typeparam>
/// <param name="t">The task to configure an awaiter for.</param>
/// <returns>An object used to await this task.</returns>
public static ConfiguredValueTaskAwaitable<T> KeepContext<T>(this ValueTask<T> t) => t.ConfigureAwait(true);
/// <summary>
/// Configures an awaiter to await a given task without capturing a context. This means that
/// subsequent code is likely to run on another context than the one the awaited task was called from.
/// You should use this method on almost every awaited method. But pay attention if your async code is coupled tightly to a user interface.
/// Equals to .ConfigureAwait(false).
/// </summary>
/// <param name="t">The task to configure an awaiter for.</param>
/// <returns>An object used to await this task.</returns>
public static ConfiguredValueTaskAwaitable FreeContext(this ValueTask t) => t.ConfigureAwait(false);
/// <summary>
/// Configures an awaiter to await a given task without capturing a context. This means that
/// subsequent code is likely to run on another context than the one the awaited task was called from.
/// You should use this method on almost every awaited method. But pay attention if your async code is coupled tightly to a user interface.
/// Equals to .ConfigureAwait(false).
/// </summary>
/// <typeparam name="T">The type of the result of the task to wait for.</typeparam>
/// <param name="t">The task to configure an awaiter for.</param>
/// <returns>An object used to await this task.</returns>
public static ConfiguredValueTaskAwaitable<T> FreeContext<T>(this ValueTask<T> t) => t.ConfigureAwait(false);
#endif
}
}