Class TaskExtensions
Extension methods to make working with System.Threading.Tasks.Task easier.
Assembly: Dalamud.dll
public static class TaskExtensions
Methods
WaitSafely(Task)
Safe alternative to Task.Wait which ensures the calling thread is not a thread pool thread.
public static void WaitSafely(this Task task)
Parameters
| Type | Name | Description |
|---|---|---|
System.Threading.Tasks.Task | task | The task to be awaited. |
GetResultSafely<T>(Task<T>)
Safe alternative to Task.Result which ensures the calling thread is not a thread pool thread.
public static T GetResultSafely<T>(this Task<T> task)
Returns
<T>: The result.
Parameters
| Type | Name | Description |
|---|---|---|
System.Threading.Tasks.Task<<T>> | task | The target task. |
Type Parameters
| Name | Description |
|---|---|
T | The type of the result. |
SuppressException(Task)
Creates a new System.Threading.Tasks.Task that resolves when <code class="paramref">task</code> completes, ignoring
exceptions thrown from the task, if any.
public static Task SuppressException(this Task task)
Returns
System.Threading.Tasks.Task: A System.Threading.Tasks.Task that completes successfully when <code class="paramref">task</code> completes in any state.
Parameters
| Type | Name | Description |
|---|---|---|
System.Threading.Tasks.Task | task | Task to await and ignore exceptions on failure. |
Remarks
Awaiting the returned System.Threading.Tasks.Task will always complete without exceptions, but awaiting
<code class="paramref">task</code> will throw exceptions if it fails, even after this function is called.
Examples
<p> Wrong use of this function
var task = TaskThrowingException();
task.SuppressException();
await TaskThrowingException(); // This line will throw.
</p> <p> Correct use of this function, if waiting for the task
await TaskThrowingException().SuppressException();
</p> <p> Fire-and-forget
If not interested in the execution state of Task (fire-and-forget), simply calling this function will do. This function consumes the task's exception, so that it won't bubble up on later garbage collection.
TaskThrowingException().SuppressException();
</p>