private void HandleTeamRibbonImport(object sender, System.EventArgs e) { this.Logger.Log(TraceEventType.Information, "Flow controller HandleTeamRibbonImport called"); Debug.Assert(this.Manager.ActiveContainer != null, "There should be an active container in the team project document manager"); Debug.Assert(this.Manager.ActiveDocument != null, "There should be an active document in the team project document manager"); ITeamProject teamProject = null; ITeamProjectPickerPresenter projectPickerPresenter = null; if (!this.Manager.ActiveDocument.IsInsertable) { this.TeamRibbonPresenter.DisplayError(FlowControllerResources.DocumentNotInsertable, string.Empty); } else if (!this.Manager.ActiveDocument.IsConnected) { projectPickerPresenter = this.Manager.ActiveContainer.Resolve <ITeamProjectPickerPresenter>(); teamProject = projectPickerPresenter.ChooseTeamProject(); } else { teamProject = this.Manager.ActiveDocument.TeamProject; } if (teamProject != null) { this.Manager.ActiveContainer.RegisterInstance <ITeamProject>(teamProject); IWorkItemQueryAndLayoutPickerWizardPresenter workItemAndLayoutPickerWizardPresenter = this.Manager.ActiveContainer.Resolve <IWorkItemQueryAndLayoutPickerWizardPresenter>(); workItemAndLayoutPickerWizardPresenter.Initialise(); workItemAndLayoutPickerWizardPresenter.Start(); QueryAndLayoutInformation queryAndLayout = workItemAndLayoutPickerWizardPresenter.QueryAndLayout; if (queryAndLayout != null) { CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); TaskScheduler scheduler = this.UnityContainer.Resolve <TaskScheduler>(); Task <WorkItemTree> runQueryTask = new Task <WorkItemTree>( () => { this.Logger.Log(TraceEventType.Information, "Starting query for work items"); WorkItemTree ans = teamProject.QueryRunner.QueryForWorkItems(queryAndLayout.Query, cancellationTokenSource.Token); this.Logger.Log(TraceEventType.Information, "Finished querying for work items"); return(ans); }, cancellationTokenSource.Token); Task queryErrorTask = runQueryTask.ContinueWith( antecedent => { this.Logger.Log(TraceEventType.Error, "Query execution failed: {0}", antecedent.Exception.InnerException.Message); }, cancellationTokenSource.Token, TaskContinuationOptions.OnlyOnFaulted, scheduler); Task queryDoneTask = runQueryTask.ContinueWith( (antecedent) => { this.Logger.Log(TraceEventType.Information, "Adding work items to document"); this.TeamRibbonPresenter.UpdateCancellableOperation( FlowControllerResources.AddingWorkItemsToDocument); if (projectPickerPresenter != null) { projectPickerPresenter.SaveTeamProject(); } workItemAndLayoutPickerWizardPresenter.SaveQueryAndLayout(); this.Manager.ActiveDocument.SaveWorkItems(antecedent.Result, queryAndLayout.Layout.FieldNames.ToArray(), cancellationTokenSource.Token); this.Manager.ActiveDocument.MapWorkItemsIntoDocument(queryAndLayout.Layout, queryAndLayout.Index, cancellationTokenSource.Token); this.Logger.Log(TraceEventType.Information, "Finished adding work items to document"); }, cancellationTokenSource.Token, TaskContinuationOptions.OnlyOnRanToCompletion, scheduler); this.AddFinalErrorHandlingTask(scheduler, runQueryTask, queryErrorTask, queryDoneTask); runQueryTask.Start(scheduler); this.TeamRibbonPresenter.StartCancellableOperation(FlowControllerResources.StartQueryExecution, cancellationTokenSource); } } this.TeamRibbonPresenter.UpdateState(); }