Skip to content

Instantly share code, notes, and snippets.

@drub0y
Last active October 17, 2017 23:19
Show Gist options
  • Save drub0y/3b0386cc02c36b31b923fc5511b41eab to your computer and use it in GitHub Desktop.
Save drub0y/3b0386cc02c36b31b923fc5511b41eab to your computer and use it in GitHub Desktop.
Continuation without unecessary allocation via static method and cached delegate
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;
}
}
@drub0y
Copy link
Author

drub0y commented Oct 17, 2017

Created in response to this Twitter discussion: https://twitter.com/reubenbond/status/920402373624791040

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment