In the end the method associated with my RelayCommand looked like the below:
private void CheckWorkItemId()
{
if (!this.WorkItemIdInvalidated || this.ProjectId <= 0
|| string.IsNullOrWhiteSpace(Settings.Default.TfsServerName))
return;
this.WorkItemTitle = WORK_ITEM_FETCH;
Task.Factory.StartNew(() => WorkItemInfoRetriever.Get(
Settings.Default.TfsServerName, this.ProjectId)).ContinueWith(t =>
{
if (null == t.Exception)
{
this.WorkItemTitle = t.Result.Title;
this.WorkItemIdInvalidated = false;
}
else
{
this.WorkItemTitle = null;
var ex = t.Exception.InnerException;
if (ex is InvalidWorkItemException)
_messageBoxService.ShowOKDispatch(ex.Message, "Invalid Work Item");
else
_messageBoxService.ShowErrorDispatch(ex.Message, "Unexpected Error");
}
ReCalculateCommands();
}, TaskScheduler.FromCurrentSynchronizationContext());
}
The salient points are:
Grabbing the current task scheduler while on the UI thread with TaskScheduler.FromCurrentSynchronizationContext, done in the ContinueWith call
Chaining in a continuation to be run when the task completes via Task.ContinueWith
Checking for exception when completed. I bypassed explicit use of AggregateException and InnerExceptions as in my case there'd be a single error or none
Read more: geoffhudik.com
QR: