/// <summary>
        /// Async task to cancel backup
        /// </summary>
        /// <param name="backupOperation"></param>
        /// <param name="token"></param>
        /// <param name="backupCompletedEvent"></param>
        /// <returns></returns>
        private async Task <TaskResult> CancelTaskAsync(IBackupOperation backupOperation, CancellationToken token, AutoResetEvent backupCompletedEvent)
        {
            // Create a task for backup cancellation request
            return(await Task.Factory.StartNew(() =>
            {
                TaskResult result = new TaskResult();
                WaitHandle[] waitHandles = new WaitHandle[2]
                {
                    backupCompletedEvent,
                    token.WaitHandle
                };

                WaitHandle.WaitAny(waitHandles);
                try
                {
                    backupOperation.CancelBackup();
                    result.TaskStatus = SqlTaskStatus.Canceled;
                }
                catch (Exception ex)
                {
                    result.TaskStatus = SqlTaskStatus.Failed;
                    result.ErrorMessage = ex.Message;
                }

                return result;
            }));
        }
        /// <summary>
        /// Create a backup task for execution and cancellation
        /// </summary>
        /// <param name="sqlTask"></param>
        /// <returns></returns>
        internal async Task <TaskResult> BackupTaskAsync(SqlTask sqlTask)
        {
            sqlTask.AddMessage(SR.Task_InProgress, SqlTaskStatus.InProgress, true);
            IBackupOperation backupOperation = sqlTask.TaskMetadata.Data as IBackupOperation;
            TaskResult       taskResult      = null;

            if (backupOperation != null)
            {
                AutoResetEvent    backupCompletedEvent = new AutoResetEvent(initialState: false);
                Task <TaskResult> performTask          = PerformTaskAsync(backupOperation);
                Task <TaskResult> cancelTask           = CancelTaskAsync(backupOperation, sqlTask.TokenSource.Token, backupCompletedEvent);
                Task <TaskResult> completedTask        = await Task.WhenAny(performTask, cancelTask);

                // Release the cancelTask
                if (completedTask == performTask)
                {
                    backupCompletedEvent.Set();
                }

                sqlTask.AddMessage(completedTask.Result.TaskStatus == SqlTaskStatus.Failed ? completedTask.Result.ErrorMessage : SR.Task_Completed,
                                   completedTask.Result.TaskStatus);
                taskResult = completedTask.Result;
            }
            else
            {
                taskResult            = new TaskResult();
                taskResult.TaskStatus = SqlTaskStatus.Failed;
            }

            return(taskResult);
        }
Ejemplo n.º 3
0
        private TaskMetadata CreateTaskMetaData(IBackupOperation data)
        {
            TaskMetadata taskMetaData = new TaskMetadata
            {
                ServerName    = "server name",
                DatabaseName  = "database name",
                Name          = "backup database",
                TaskOperation = data
            };

            return(taskMetaData);
        }
 /// <summary>
 /// Async task to execute backup
 /// </summary>
 /// <param name="backupOperation"></param>
 /// <returns></returns>
 private async Task <TaskResult> PerformTaskAsync(IBackupOperation backupOperation)
 {
     // Create a task to perform backup
     return(await Task.Factory.StartNew(() =>
     {
         TaskResult result = new TaskResult();
         try
         {
             backupOperation.PerformBackup();
             result.TaskStatus = SqlTaskStatus.Succeeded;
         }
         catch (Exception ex)
         {
             result.TaskStatus = SqlTaskStatus.Failed;
             result.ErrorMessage = ex.Message;
             if (ex.InnerException != null)
             {
                 result.ErrorMessage += System.Environment.NewLine + ex.InnerException.Message;
             }
         }
         return result;
     }));
 }