Created
March 4, 2022 10:05
-
-
Save pradeepn/d5f7e9d94ca1a17c0dd91df1cd2acdaa 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 static class TaskHelper | |
{ | |
/// <summary> | |
/// Runs a TPL Task fire-and-forget style, the right way - in the | |
/// background, separate from the current thread, with no risk | |
/// of it trying to rejoin the current thread. | |
/// </summary> | |
public static void RunInBackground(this Task task, Action action) | |
{ | |
task.Run(action).ConfigureAwait(false); | |
} | |
/// <summary> | |
/// Runs a task fire-and-forget style and notifies the TPL that this | |
/// will not need a Thread to resume on for a long time, or that there | |
/// are multiple gaps in thread use that may be long. | |
/// Use for example when talking to a slow webservice. | |
/// </summary> | |
public static void LongRunInBackground(this Task task, Action action) | |
{ | |
task.Factory.StartNew(action, TaskCreationOptions.LongRunning) | |
.ConfigureAwait(false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment