Last active
October 17, 2017 23:19
-
-
Save drub0y/3b0386cc02c36b31b923fc5511b41eab to your computer and use it in GitHub Desktop.
Continuation without unecessary allocation via static method and cached delegate
This file contains hidden or 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 static class TaskExtensions | |
{ | |
private static Action<Task> IgnorerContinuationAction = IgnorerContinuation; | |
public static void Ignore(this Task task) | |
{ | |
if (task.IsCompleted) | |
{ | |
var exception = task.Exception; | |
} | |
else | |
{ | |
task.ContinueWith( | |
IgnorerContinuationAction, | |
CancellationToken.None, | |
TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously, | |
TaskScheduler.Default); | |
} | |
} | |
private static void IgnorerContinuation(Task task) | |
{ | |
var exception = task.Exception; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Created in response to this Twitter discussion: https://twitter.com/reubenbond/status/920402373624791040