forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSynchronizationContextSwitcher.cs
More file actions
64 lines (59 loc) · 2.79 KB
/
SynchronizationContextSwitcher.cs
File metadata and controls
64 lines (59 loc) · 2.79 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Npgsql
{
/// <summary>
/// Utility class for temporarily switching <see cref="SynchronizationContext"/> implementations.
///
/// This mechanism is used to temporarily set the current synchronization context to null while
/// executing Npgsql code, making all await continuations execute on the thread pool. This replaces
/// the need to place ConfigureAwait(false) everywhere, and should be used in all surface async methods,
/// without exception.
/// </summary>
/// <remarks>
/// Copied and slightly adjusted from Stephen Cleary's Nito.AsyncEx package
/// (https://github.com/StephenCleary/AsyncEx/blob/master/src/Nito.AsyncEx.Tasks/SynchronizationContextSwitcher.cs)
/// </remarks>
sealed class SynchronizationContextSwitcher : IDisposable
{
/// <summary>
/// The previous <see cref="SynchronizationContext"/>.
/// </summary>
private readonly SynchronizationContext _oldContext;
/// <summary>
/// Initializes a new instance of the <see cref="SynchronizationContextSwitcher"/> class, installing the new <see cref="SynchronizationContext"/>.
/// </summary>
/// <param name="newContext">The new <see cref="SynchronizationContext"/>. This can be <c>null</c> to remove an existing <see cref="SynchronizationContext"/>.</param>
internal SynchronizationContextSwitcher(SynchronizationContext newContext)
{
_oldContext = SynchronizationContext.Current;
SynchronizationContext.SetSynchronizationContext(newContext);
}
/// <summary>
/// Restores the old <see cref="SynchronizationContext"/>.
/// </summary>
public void Dispose() => SynchronizationContext.SetSynchronizationContext(_oldContext);
/// <summary>
/// Executes a synchronous delegate without the current <see cref="SynchronizationContext"/>. The current context is restored when this function returns.
/// </summary>
/// <param name="action">The delegate to execute.</param>
public static void NoContext(Action action)
{
using (new SynchronizationContextSwitcher(null))
action();
}
/// <summary>
/// Executes a synchronous or asynchronous delegate without the current <see cref="SynchronizationContext"/>. The current context is restored when this function synchronously returns.
/// </summary>
/// <param name="action">The delegate to execute.</param>
public static T NoContext<T>(Func<T> action)
{
using (new SynchronizationContextSwitcher(null))
return action();
}
}
}