// ReSharper disable CheckNamespace namespace System.Threading.Tasks { /// /// Extension methods for the Task object. /// public static class TaskExtensionsT { /// /// Awaits a task without blocking the main thread. /// /// Primarily used to replace async void scenarios such as ctor's and ICommands. /// The result type /// The task to be awaited public static void Await(this Task task) { task.Await(null, null, false); } /// /// Awaits a task without blocking the main thread. /// /// Primarily used to replace async void scenarios such as ctor's and ICommands. /// The result type /// The task to be awaited /// Configures an awaiter used to await this task public static void Await(this Task task, bool configureAwait) { task.Await(null, null, configureAwait); } /// /// Awaits a task without blocking the main thread. /// /// Primarily used to replace async void scenarios such as ctor's and ICommands. /// The result type /// The task to be awaited /// The action to perform when the task is complete. public static void Await(this Task task, Action completedCallback) { task.Await(completedCallback, null, false); } /// /// Awaits a task without blocking the main thread. /// /// Primarily used to replace async void scenarios such as ctor's and ICommands. /// The result type /// The task to be awaited /// The action to perform when the task is complete. /// The action to perform when an error occurs executing the task. public static void Await(this Task task, Action completedCallback, Action errorCallback) { task.Await(completedCallback, errorCallback, false); } /// /// Awaits a task without blocking the main thread. /// /// Primarily used to replace async void scenarios such as ctor's and ICommands. /// The result type /// The task to be awaited /// The action to perform when an error occurs executing the task. public static void Await(this Task task, Action errorCallback) { task.Await(null, errorCallback, false); } /// /// Awaits a task without blocking the main thread. /// /// Primarily used to replace async void scenarios such as ctor's and ICommands. /// The result type /// The task to be awaited /// The action to perform when the task is complete. /// The action to perform when an error occurs executing the task. /// Configures an awaiter used to await this task public async static void Await(this Task task, Action? completedCallback, Action? errorCallback, bool configureAwait) { try { var result = await task.ConfigureAwait(configureAwait); completedCallback?.Invoke(result); } catch (Exception ex) { errorCallback?.Invoke(ex); } } } }