/Main/src/DynamicDataDisplay/Common/Auxiliary/TaskExtensions.cs
C# | 51 lines | 0 code | 0 blank | 51 comment | 0 complexity | 9b5de9450db4365a8e30ffd9a113c391 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
1/*using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6using System.Diagnostics; 7using System.Windows.Threading; 8 9namespace Microsoft.Research.DynamicDataDisplay.Common.Auxiliary 10{ 11 public static class TaskExtensions 12 { 13 /// <summary> 14 /// Logs exceptions that occur during task execution. 15 /// </summary> 16 /// <param name="task">The task.</param> 17 /// <returns></returns> 18 public static Task WithExceptionLogging(this Task task) 19 { 20 return task.ContinueWith(t => 21 { 22 var exception = t.Exception; 23 if (exception != null) 24 { 25 if (exception.InnerException != null) 26 exception = exception.InnerException; 27 28 Debug.WriteLine("Failure in async task: " + exception.Message); 29 } 30 }, TaskContinuationKind.OnFailed); 31 } 32 33 /// <summary> 34 /// Rethrows exceptions thrown during task execution in thespecified dispatcher thread. 35 /// </summary> 36 /// <param name="task">The task.</param> 37 /// <param name="dispatcher">The dispatcher.</param> 38 /// <returns></returns> 39 public static Task WithExceptionThrowingInDispatcher(this Task task, Dispatcher dispatcher) 40 { 41 return task.ContinueWith(t => 42 { 43 dispatcher.BeginInvoke(() => 44 { 45 throw t.Exception; 46 }, DispatcherPriority.Send); 47 }, TaskContinuationKind.OnFailed); 48 } 49 } 50} 51*/