Created
February 1, 2021 11:43
-
-
Save Dankann/540aff1b1614b33c66abb0ee60b32f22 to your computer and use it in GitHub Desktop.
Task Extensions
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
using System; | |
using System.Threading; | |
using System.Threading.Tasks; | |
public static class TaskExtensions | |
{ | |
public static async void FireAndForgetAsync(this Task task) | |
{ | |
try | |
{ | |
await task; | |
} | |
catch | |
{ | |
if (!task.IsCanceled) | |
throw; | |
} | |
} | |
public static async Task WaitUntilAsync(Func<bool> predicate, CancellationToken token = default) | |
{ | |
try | |
{ | |
while (!predicate() && !token.IsCancellationRequested) | |
await Task.Yield(); | |
} | |
catch (NullReferenceException) | |
{ | |
throw; | |
} | |
} | |
public static async Task WaitWhileAsync(Func<bool> predicate, CancellationToken token = default) | |
{ | |
try | |
{ | |
while (predicate() && !token.IsCancellationRequested) | |
await Task.Yield(); | |
} | |
catch (NullReferenceException) | |
{ | |
throw; | |
} | |
} | |
public static async Task DelayActionAsync(Action action, int milliseconds, CancellationToken token = default) | |
{ | |
try | |
{ | |
await Task.Delay(milliseconds, token); | |
action(); | |
} | |
catch (NullReferenceException) | |
{ | |
throw; | |
} | |
} | |
public static async Task DelayActionFramesAsync(Action action, int frames, CancellationToken token = default) | |
{ | |
try | |
{ | |
for (int i = 0; i < frames && !token.IsCancellationRequested; i++) | |
await Task.Yield(); | |
action(); | |
} | |
catch | |
{ | |
throw; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment