コード例 #1
0
ファイル: TaskHelper.cs プロジェクト: yousser/201505-MVA
        /// <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);
            }
        }
コード例 #2
0
ファイル: ViewModel.cs プロジェクト: MuffPotter/201505-MVA
        /// <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;
            }
        }
コード例 #3
0
ファイル: TaskHelper.cs プロジェクト: yousser/201505-MVA
        /// <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();
            }
        }
コード例 #4
0
ファイル: TaskHelper.cs プロジェクト: MuffPotter/201505-MVA
        /// <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);
            }
        }
コード例 #5
0
ファイル: TaskHelper.cs プロジェクト: MuffPotter/201505-MVA
        /// <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();
            }
        }