Created
May 23, 2018 13:52
-
-
Save NtFreX/fb62b82849f6cad14690328906407529 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 struct NonThrowingAwaitable | |
{ | |
private readonly Task _task; | |
private readonly bool _continueOnCapturedContext; | |
public NonThrowingAwaitable(Task task, bool continueOnCapturedContext) | |
{ | |
_task = task; | |
_continueOnCapturedContext = continueOnCapturedContext; | |
} | |
public NonThrowingAwaiter GetAwaiter() => new NonThrowingAwaiter(_task, _continueOnCapturedContext); | |
public struct NonThrowingAwaiter : INotifyCompletion | |
{ | |
private ConfiguredTaskAwaitable.ConfiguredTaskAwaiter _innerAwaiter; | |
public NonThrowingAwaiter(Task task, bool continueOnCapturedContext) | |
: this(task.ConfigureAwait(continueOnCapturedContext).GetAwaiter()) { } | |
private NonThrowingAwaiter(ConfiguredTaskAwaitable.ConfiguredTaskAwaiter innerAwaiter) | |
{ | |
_innerAwaiter = innerAwaiter; | |
} | |
public void GetResult() { } | |
public bool IsCompleted => _innerAwaiter.IsCompleted; | |
public void OnCompleted(Action continuation) => _innerAwaiter.OnCompleted(continuation); | |
} | |
} | |
public static class TaskExtensions | |
{ | |
public static NonThrowingAwaitable NonThrowing(this Task task, bool continueOnCapturedContext = true) | |
{ | |
return new NonThrowingAwaitable(task, continueOnCapturedContext); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment