/// <summary> /// Executes a task and handles any errors that occur. /// </summary> /// <param name="action"> /// The <see cref="Action"/> to execute. /// </param> /// <param name="options"> /// A <see cref="TaskRunOptions"/> instance that specifies options for running the task or <see langword="null"/> /// to use <see cref="TaskRunOptions.Default"/>. /// </param> /// <returns> /// A <see cref="Task"/> that represents the wrapped execution of the inner action. /// </returns> static public async Task RunWithErrorHandling(Action action, TaskRunOptions options = null) { // Validate if (action == null) { throw new ArgumentNullException("action"); } // Options if (options == null) { options = TaskRunOptions.Default; } // Handle failure try { // Custom scheduler if (options.Scheduler != null) { await new TaskFactory(options.Scheduler).StartNew(action); } else { await Task.Factory.StartNew(action); } } catch (Exception ex) { await DisplayErrorAsync(ex, options); } }
/// <summary> /// Executes a task and handles any errors that occur. /// </summary> /// <param name="taskFunction"> /// A function that yields the <see cref="Task"/> to execute. /// </param> /// <param name="options"> /// A <see cref="TaskRunOptions"/> instance that specifies options for running the task or <see langword="null"/> /// to use <see cref="TaskRunOptions.Default"/>. /// </param> /// <returns> /// A <see cref="Task"/> that represents the wrapped execution of the inner task. /// </returns> protected async Task RunWithErrorHandling(Func<Task> taskFunction, TaskRunOptions options = null) { // Busy? if (options.IsBusy) { isBusy = true; } // Use task helper await TaskHelper.RunWithErrorHandling(taskFunction, options); // No longer busy? if (options.IsBusy) { isBusy = false; } }
/// <summary> /// Display information about the error if error display is turned on. /// </summary> /// <param name="ex"> /// The exception. /// </param> /// <param name="options"> /// The <see cref="TaskRunOptions"/> that controls how errors are displayed. /// </param> /// <returns> /// A <see cref="Task"/> that represents the operation. /// </returns> static private async Task DisplayErrorAsync(Exception ex, TaskRunOptions options) { // Show failures? if (options.DisplayFailures) { // Build failure message string message; if (options.DisplayExceptionInfo) { message = string.Format("{0} \r\n\r\n{1}", options.FailureMessge, ex.Message); } else { message = options.FailureMessge; } // Show failure message await new MessageDialog(message).ShowAsync(); } }
/// <summary> /// Executes a task and handles any errors that occur. /// </summary> /// <param name="action"> /// The <see cref="Action"/> to execute. /// </param> /// <param name="options"> /// A <see cref="TaskRunOptions"/> instance that specifies options for running the task or <see langword="null"/> /// to use <see cref="TaskRunOptions.Default"/>. /// </param> /// <returns> /// A <see cref="Task"/> that represents the wrapped execution of the inner action. /// </returns> static public async Task RunWithErrorHandling(Action action, TaskRunOptions options = null) { // Validate if (action == null) throw new ArgumentNullException("action"); // Options if (options == null) { options = TaskRunOptions.Default; } // Handle failure try { // Custom scheduler if (options.Scheduler != null) { await new TaskFactory(options.Scheduler).StartNew(action); } else { await Task.Factory.StartNew(action); } } catch (Exception ex) { await DisplayErrorAsync(ex, options); } }