PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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