Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/DynamicDataDisplay/Common/Auxiliary/TaskExtensions.cs @ 12503

Last change on this file since 12503 was 12503, checked in by aballeit, 9 years ago

#2283 added GUI and charts; fixed MCTS

File size: 1.4 KB
Line 
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*/
Note: See TracBrowser for help on using the repository browser.