-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNullSynchronizationContext.cs
More file actions
33 lines (31 loc) · 1.34 KB
/
NullSynchronizationContext.cs
File metadata and controls
33 lines (31 loc) · 1.34 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
namespace Ramstack.FileSystem.Amazon.Utilities;
/// <summary>
/// Provides a mechanism to temporarily set the <see cref="SynchronizationContext"/> to <see langword="null"/>.
/// </summary>
internal static class NullSynchronizationContext
{
/// <summary>
/// Sets the current <see cref="SynchronizationContext"/> to <see langword="null"/>
/// and returns a disposable scope that restores the original context when disposed.
/// </summary>
/// <returns>
/// A <see cref="ContextScope"/> struct that restores the original <see cref="SynchronizationContext"/> when disposed.
/// </returns>
public static ContextScope CreateScope()
{
var context = SynchronizationContext.Current;
SynchronizationContext.SetSynchronizationContext(null);
return new ContextScope(context);
}
/// <summary>
/// A disposable struct that restores the original <see cref="SynchronizationContext"/> when disposed.
/// </summary>
public readonly struct ContextScope(SynchronizationContext? context) : IDisposable
{
/// <summary>
/// Restores the original <see cref="SynchronizationContext"/> that was present when the scope was created.
/// </summary>
public void Dispose() =>
SynchronizationContext.SetSynchronizationContext(context);
}
}