forked from PostgreSQLCopyHelper/PostgreSQLCopyHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
69 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...pyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Utils/NoSynchronizationContextScope.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Code taken from https://github.com/npgsql/npgsql/blob/dev/src/Npgsql/NoSynchronizationContextScope.cs | ||
|
||
using System; | ||
using System.Threading; | ||
|
||
namespace PostgreSQLCopyHelper.Utils | ||
{ | ||
/// <summary> | ||
/// 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. | ||
/// | ||
/// Warning: do not use this directly in async methods, use it in sync wrappers of async methods | ||
/// (see https://github.com/npgsql/npgsql/issues/1593) | ||
/// </summary> | ||
/// <remarks> | ||
/// http://stackoverflow.com/a/28307965/640325 | ||
/// </remarks> | ||
internal static class NoSynchronizationContextScope | ||
{ | ||
internal static Disposable Enter() | ||
{ | ||
var sc = SynchronizationContext.Current; | ||
SynchronizationContext.SetSynchronizationContext(null); | ||
return new Disposable(sc); | ||
} | ||
|
||
internal struct Disposable : IDisposable | ||
{ | ||
private readonly SynchronizationContext _synchronizationContext; | ||
|
||
internal Disposable(SynchronizationContext synchronizationContext) | ||
=> _synchronizationContext = synchronizationContext; | ||
|
||
public void Dispose() | ||
=> SynchronizationContext.SetSynchronizationContext(_synchronizationContext); | ||
} | ||
} | ||
} |