Ejemplo n.º 1
0
        private void _RunOperation(FormOperationController operation)
        {
            _runningCount++;
            _settingsForm.ConditionalSettingDisable(_anyRunning);
            operation.ToQueuedState();
            var  cancelTokenSource = new CancellationTokenSource();// Prepare task
            Task task = Task.Run(() =>
            {
                operation.ToRunState();
                ProgramHelper.DoWork(operation);
            }, cancelTokenSource.Token).ContinueWith(t =>
            {
                cancelTokenSource.Dispose();
                _runningCount--;
                // Exception management
                if (t.IsFaulted)
                {
                    if (t.Exception != null)
                    {
                        if (t.Exception.InnerExceptions.Count > 1)
                        {
                            operation.UpdateProgress("Something terrible has happened");
                        }
                        else
                        {
                            Exception ex = t.Exception.InnerExceptions[0];
                            if (!(ex is EncompassException))
                            {
                                operation.UpdateProgress("Something terrible has happened");
                            }
                            if (!(ex.InnerException is OperationCanceledException))
                            {
                                operation.UpdateProgress(ex.GetFinalException().Message);
                            }
                            else
                            {
                                operation.UpdateProgress("Operation Aborted");
                            }
                        }
                    }
                    else
                    {
                        operation.UpdateProgress("Something terrible has happened");
                    }
                }
                if (operation.State == OperationState.ClearOnCancel)
                {
                    operatorBindingSource.Remove(operation);
                }
                else
                {
                    if (t.IsFaulted)
                    {
                        operation.ToUnCompleteState();
                    }
                    else if (t.IsCanceled)
                    {
                        operation.UpdateProgress("Operation Aborted");
                        operation.ToUnCompleteState();
                    }
                    else if (t.IsCompleted)
                    {
                        operation.ToCompleteState();
                    }
                    else
                    {
                        operation.UpdateProgress("Something terrible has happened");
                        operation.ToUnCompleteState();
                    }
                }
                ProgramHelper.GenerateTraceFile(operation);
                RunNextOperation();
            }, TaskScheduler.FromCurrentSynchronizationContext());

            operation.SetContext(cancelTokenSource, task);
        }