Skip to content

Instantly share code, notes, and snippets.

@thomaslevesque
Last active December 27, 2015 12:29
Show Gist options
  • Save thomaslevesque/7325901 to your computer and use it in GitHub Desktop.
Save thomaslevesque/7325901 to your computer and use it in GitHub Desktop.
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