Last active
December 27, 2015 12:29
-
-
Save thomaslevesque/7325901 to your computer and use it in GitHub Desktop.
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
public class Deferral : IDisposable | |
{ | |
private TaskCompletionSource<object> _completionSource = new TaskCompletionSource<object>(); | |
~Deferral() | |
{ | |
Dispose(false); | |
} | |
public void Dispose() | |
{ | |
Dispose(true); | |
} | |
private void Dispose(bool disposing) | |
{ | |
if (disposing) | |
{ | |
_completionSource.TrySetResult(null); | |
GC.SuppressFinalize(this); | |
} | |
else | |
{ | |
_completionSource.SetException(new DeferralNotCompletedException()); | |
} | |
} | |
public Task Task | |
{ | |
get { return _completionSource.Task; } | |
} | |
} | |
public class DeferralNotCompletedException : Exception | |
{ | |
public DeferralNotCompletedException(string message = "Deferral was not completed", Exception inner = null) | |
: base(message, inner) | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment