using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Windows.Threading;
namespace Microsoft.Research.DynamicDataDisplay.Common.Auxiliary
{
public static class TaskExtensions
{
///
/// Logs exceptions that occur during task execution.
///
/// The task.
///
public static Task WithExceptionLogging(this Task task)
{
return task.ContinueWith(t =>
{
var exception = t.Exception;
if (exception != null)
{
if (exception.InnerException != null)
exception = (AggregateException)exception.InnerException;
Debug.WriteLine("Failure in async task: " + exception.Message);
}
});
}
///
/// Rethrows exceptions thrown during task execution in thespecified dispatcher thread.
///
/// The task.
/// The dispatcher.
///
public static Task WithExceptionThrowingInDispatcher(this Task task, Dispatcher dispatcher)
{
return task.ContinueWith(t =>
{
dispatcher.BeginInvoke(() =>
{
throw t.Exception;
}, DispatcherPriority.Send);
});
}
}
}