Advertisement
andrew4582

SignalR TaskAsyncHelper

Dec 11th, 2014
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 47.35 KB | None | 0 0
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
  2.  
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Diagnostics.CodeAnalysis;
  8. using System.Globalization;
  9. using System.Linq;
  10. using System.Reflection;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using Microsoft.AspNet.SignalR.Infrastructure;
  14.  
  15. namespace Microsoft.AspNet.SignalR
  16. {
  17.     internal static class TaskAsyncHelper
  18.     {
  19.         private static readonly Task _emptyTask = MakeTask<object>(null);
  20.         private static readonly Task<bool> _trueTask = MakeTask<bool>(true);
  21.         private static readonly Task<bool> _falseTask = MakeTask<bool>(false);
  22.  
  23.         private static Task<T> MakeTask<T>(T value)
  24.         {
  25.             return FromResult<T>(value);
  26.         }
  27.  
  28.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  29.         public static Task Empty
  30.         {
  31.             get
  32.             {
  33.                 return _emptyTask;
  34.             }
  35.         }
  36.  
  37.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  38.         public static Task<bool> True
  39.         {
  40.             get
  41.             {
  42.                 return _trueTask;
  43.             }
  44.         }
  45.  
  46.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  47.         public static Task<bool> False
  48.         {
  49.             get
  50.             {
  51.                 return _falseTask;
  52.             }
  53.         }
  54.  
  55.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  56.         public static Task OrEmpty(this Task task)
  57.         {
  58.             return task ?? Empty;
  59.         }
  60.  
  61.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  62.         public static Task<T> OrEmpty<T>(this Task<T> task)
  63.         {
  64.             return task ?? TaskCache<T>.Empty;
  65.         }
  66.  
  67.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  68.         [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions are set in a tcs")]
  69.         public static Task FromAsync(Func<AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod, object state)
  70.         {
  71.             try
  72.             {
  73.                 return Task.Factory.FromAsync(beginMethod, endMethod, state);
  74.             }
  75.             catch (Exception ex)
  76.             {
  77.                 return TaskAsyncHelper.FromError(ex);
  78.             }
  79.         }
  80.  
  81.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  82.         [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions are set in a tcs")]
  83.         public static Task<T> FromAsync<T>(Func<AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, T> endMethod, object state)
  84.         {
  85.             try
  86.             {
  87.                 return Task.Factory.FromAsync<T>(beginMethod, endMethod, state);
  88.             }
  89.             catch (Exception ex)
  90.             {
  91.                 return TaskAsyncHelper.FromError<T>(ex);
  92.             }
  93.         }
  94.  
  95.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  96.         public static TTask Catch<TTask>(this TTask task) where TTask : Task
  97.         {
  98.             return Catch(task, ex => { });
  99.         }
  100.  
  101. #if PERFCOUNTERS
  102.         public static TTask Catch<TTask>(this TTask task, params IPerformanceCounter[] counters) where TTask : Task
  103.         {
  104.             return Catch(task, _ =>
  105.                 {
  106.                     if (counters == null)
  107.                     {
  108.                         return;
  109.                     }
  110.                     for (var i = 0; i < counters.Length; i++)
  111.                     {
  112.                         counters[i].Increment();
  113.                     }
  114.                 });
  115.         }
  116. #endif
  117.  
  118.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  119.         public static TTask Catch<TTask>(this TTask task, Action<AggregateException, object> handler, object state) where TTask : Task
  120.         {
  121.             if (task != null && task.Status != TaskStatus.RanToCompletion)
  122.             {
  123.                 if (task.Status == TaskStatus.Faulted)
  124.                 {
  125.                     ExecuteOnFaulted(handler, state, task.Exception);
  126.                 }
  127.                 else
  128.                 {
  129.                     AttachFaultedContinuation<TTask>(task, handler, state);
  130.                 }
  131.             }
  132.  
  133.             return task;
  134.         }
  135.  
  136.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  137.         private static void AttachFaultedContinuation<TTask>(TTask task, Action<AggregateException, object> handler, object state) where TTask : Task
  138.         {
  139.             task.ContinueWithPreservedCulture(innerTask =>
  140.             {
  141.                 ExecuteOnFaulted(handler, state, innerTask.Exception);
  142.             },
  143.             TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously);
  144.         }
  145.  
  146.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  147.         private static void ExecuteOnFaulted(Action<AggregateException, object> handler, object state, AggregateException exception)
  148.         {
  149.             // observe Exception
  150. #if !PORTABLE && !NETFX_CORE && !__ANDROID__ && !IOS
  151.             Trace.TraceWarning("SignalR exception thrown by Task: {0}", exception);
  152. #endif
  153.             handler(exception, state);
  154.         }
  155.  
  156.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  157.         public static TTask Catch<TTask>(this TTask task, Action<AggregateException> handler) where TTask : Task
  158.         {
  159.             return task.Catch((ex, state) => ((Action<AggregateException>)state).Invoke(ex),
  160.                               handler);
  161.         }
  162.  
  163.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  164.         [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions are set in a tcs")]
  165.         public static Task ContinueWithNotComplete(this Task task, Action action)
  166.         {
  167.             switch (task.Status)
  168.             {
  169.                 case TaskStatus.Faulted:
  170.                 case TaskStatus.Canceled:
  171.                     try
  172.                     {
  173.                         action();
  174.                         return task;
  175.                     }
  176.                     catch (Exception e)
  177.                     {
  178.                         return FromError(e);
  179.                     }
  180.                 case TaskStatus.RanToCompletion:
  181.                     return task;
  182.                 default:
  183.                     var tcs = new TaskCompletionSource<object>();
  184.  
  185.                     task.ContinueWithPreservedCulture(t =>
  186.                     {
  187.                         if (t.IsFaulted || t.IsCanceled)
  188.                         {
  189.                             try
  190.                             {
  191.                                 action();
  192.  
  193.                                 if (t.IsFaulted)
  194.                                 {
  195.                                     tcs.TrySetUnwrappedException(t.Exception);
  196.                                 }
  197.                                 else
  198.                                 {
  199.                                     tcs.TrySetCanceled();
  200.                                 }
  201.                             }
  202.                             catch (Exception e)
  203.                             {
  204.                                 tcs.TrySetException(e);
  205.                             }
  206.                         }
  207.                         else
  208.                         {
  209.                             tcs.TrySetResult(null);
  210.                         }
  211.                     },
  212.                     TaskContinuationOptions.ExecuteSynchronously);
  213.  
  214.                     return tcs.Task;
  215.             }
  216.         }
  217.  
  218.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  219.         public static void ContinueWithNotComplete(this Task task, TaskCompletionSource<object> tcs)
  220.         {
  221.             task.ContinueWithPreservedCulture(t =>
  222.             {
  223.                 if (t.IsFaulted)
  224.                 {
  225.                     tcs.SetUnwrappedException(t.Exception);
  226.                 }
  227.                 else if (t.IsCanceled)
  228.                 {
  229.                     tcs.SetCanceled();
  230.                 }
  231.             },
  232.             TaskContinuationOptions.NotOnRanToCompletion);
  233.         }
  234.  
  235.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  236.         public static Task ContinueWith(this Task task, TaskCompletionSource<object> tcs)
  237.         {
  238.             task.ContinueWithPreservedCulture(t =>
  239.             {
  240.                 if (t.IsFaulted)
  241.                 {
  242.                     tcs.TrySetUnwrappedException(t.Exception);
  243.                 }
  244.                 else if (t.IsCanceled)
  245.                 {
  246.                     tcs.TrySetCanceled();
  247.                 }
  248.                 else
  249.                 {
  250.                     tcs.TrySetResult(null);
  251.                 }
  252.             },
  253.             TaskContinuationOptions.ExecuteSynchronously);
  254.  
  255.             return tcs.Task;
  256.         }
  257.  
  258.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  259.         public static void ContinueWith<T>(this Task<T> task, TaskCompletionSource<T> tcs)
  260.         {
  261.             task.ContinueWithPreservedCulture(t =>
  262.             {
  263.                 if (t.IsFaulted)
  264.                 {
  265.                     tcs.TrySetUnwrappedException(t.Exception);
  266.                 }
  267.                 else if (t.IsCanceled)
  268.                 {
  269.                     tcs.TrySetCanceled();
  270.                 }
  271.                 else
  272.                 {
  273.                     tcs.TrySetResult(t.Result);
  274.                 }
  275.             });
  276.         }
  277.  
  278.         // Then extesions
  279.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  280.         public static Task Then(this Task task, Action successor)
  281.         {
  282.             switch (task.Status)
  283.             {
  284.                 case TaskStatus.Faulted:
  285.                 case TaskStatus.Canceled:
  286.                     return task;
  287.  
  288.                 case TaskStatus.RanToCompletion:
  289.                     return FromMethod(successor);
  290.  
  291.                 default:
  292.                     return RunTask(task, successor);
  293.             }
  294.         }
  295.  
  296.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  297.         public static Task<TResult> Then<TResult>(this Task task, Func<TResult> successor)
  298.         {
  299.             switch (task.Status)
  300.             {
  301.                 case TaskStatus.Faulted:
  302.                     return FromError<TResult>(task.Exception);
  303.  
  304.                 case TaskStatus.Canceled:
  305.                     return Canceled<TResult>();
  306.  
  307.                 case TaskStatus.RanToCompletion:
  308.                     return FromMethod(successor);
  309.  
  310.                 default:
  311.                     return TaskRunners<object, TResult>.RunTask(task, successor);
  312.             }
  313.         }
  314.  
  315.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  316.         public static Task Then<T1>(this Task task, Action<T1> successor, T1 arg1)
  317.         {
  318.             switch (task.Status)
  319.             {
  320.                 case TaskStatus.Faulted:
  321.                 case TaskStatus.Canceled:
  322.                     return task;
  323.  
  324.                 case TaskStatus.RanToCompletion:
  325.                     return FromMethod(successor, arg1);
  326.  
  327.                 default:
  328.                     return GenericDelegates<object, object, T1, object, object>.ThenWithArgs(task, successor, arg1);
  329.             }
  330.         }
  331.  
  332.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  333.         public static Task Then<T1, T2>(this Task task, Action<T1, T2> successor, T1 arg1, T2 arg2)
  334.         {
  335.             switch (task.Status)
  336.             {
  337.                 case TaskStatus.Faulted:
  338.                 case TaskStatus.Canceled:
  339.                     return task;
  340.  
  341.                 case TaskStatus.RanToCompletion:
  342.                     return FromMethod(successor, arg1, arg2);
  343.  
  344.                 default:
  345.                     return GenericDelegates<object, object, T1, T2, object>.ThenWithArgs(task, successor, arg1, arg2);
  346.             }
  347.         }
  348.  
  349.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  350.         public static Task Then<T1>(this Task task, Func<T1, Task> successor, T1 arg1)
  351.         {
  352.             switch (task.Status)
  353.             {
  354.                 case TaskStatus.Faulted:
  355.                 case TaskStatus.Canceled:
  356.                     return task;
  357.  
  358.                 case TaskStatus.RanToCompletion:
  359.                     return FromMethod(successor, arg1);
  360.  
  361.                 default:
  362.                     return GenericDelegates<object, Task, T1, object, object>.ThenWithArgs(task, successor, arg1)
  363.                                                                      .FastUnwrap();
  364.             }
  365.         }
  366.  
  367.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  368.         public static Task Then<T1, T2>(this Task task, Func<T1, T2, Task> successor, T1 arg1, T2 arg2)
  369.         {
  370.             switch (task.Status)
  371.             {
  372.                 case TaskStatus.Faulted:
  373.                 case TaskStatus.Canceled:
  374.                     return task;
  375.  
  376.                 case TaskStatus.RanToCompletion:
  377.                     return FromMethod(successor, arg1, arg2);
  378.  
  379.                 default:
  380.                     return GenericDelegates<object, Task, T1, T2, object>.ThenWithArgs(task, successor, arg1, arg2)
  381.                                                                  .FastUnwrap();
  382.             }
  383.         }
  384.  
  385.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  386.         public static Task Then<T1, T2, T3>(this Task task, Func<T1, T2, T3, Task> successor, T1 arg1, T2 arg2, T3 arg3)
  387.         {
  388.             switch (task.Status)
  389.             {
  390.                 case TaskStatus.Faulted:
  391.                 case TaskStatus.Canceled:
  392.                     return task;
  393.  
  394.                 case TaskStatus.RanToCompletion:
  395.                     return FromMethod(successor, arg1, arg2, arg3);
  396.  
  397.                 default:
  398.                     return GenericDelegates<object, Task, T1, T2, T3>.ThenWithArgs(task, successor, arg1, arg2, arg3)
  399.                                                                  .FastUnwrap();
  400.             }
  401.         }
  402.  
  403.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  404.         public static Task<TResult> Then<T, TResult>(this Task<T> task, Func<T, Task<TResult>> successor)
  405.         {
  406.             switch (task.Status)
  407.             {
  408.                 case TaskStatus.Faulted:
  409.                     return FromError<TResult>(task.Exception);
  410.  
  411.                 case TaskStatus.Canceled:
  412.                     return Canceled<TResult>();
  413.  
  414.                 case TaskStatus.RanToCompletion:
  415.                     return FromMethod(successor, task.Result);
  416.  
  417.                 default:
  418.                     return TaskRunners<T, Task<TResult>>.RunTask(task, t => successor(t.Result))
  419.                                                         .FastUnwrap();
  420.             }
  421.         }
  422.  
  423.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  424.         public static Task<TResult> Then<T, TResult>(this Task<T> task, Func<T, TResult> successor)
  425.         {
  426.             switch (task.Status)
  427.             {
  428.                 case TaskStatus.Faulted:
  429.                     return FromError<TResult>(task.Exception);
  430.  
  431.                 case TaskStatus.Canceled:
  432.                     return Canceled<TResult>();
  433.  
  434.                 case TaskStatus.RanToCompletion:
  435.                     return FromMethod(successor, task.Result);
  436.  
  437.                 default:
  438.                     return TaskRunners<T, TResult>.RunTask(task, t => successor(t.Result));
  439.             }
  440.         }
  441.  
  442.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  443.         public static Task<TResult> Then<T, T1, TResult>(this Task<T> task, Func<T, T1, TResult> successor, T1 arg1)
  444.         {
  445.             switch (task.Status)
  446.             {
  447.                 case TaskStatus.Faulted:
  448.                     return FromError<TResult>(task.Exception);
  449.  
  450.                 case TaskStatus.Canceled:
  451.                     return Canceled<TResult>();
  452.  
  453.                 case TaskStatus.RanToCompletion:
  454.                     return FromMethod(successor, task.Result, arg1);
  455.  
  456.                 default:
  457.                     return GenericDelegates<T, TResult, T1, object, object>.ThenWithArgs(task, successor, arg1);
  458.             }
  459.         }
  460.  
  461.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  462.         public static Task Then(this Task task, Func<Task> successor)
  463.         {
  464.             switch (task.Status)
  465.             {
  466.                 case TaskStatus.Faulted:
  467.                 case TaskStatus.Canceled:
  468.                     return task;
  469.  
  470.                 case TaskStatus.RanToCompletion:
  471.                     return FromMethod(successor);
  472.  
  473.                 default:
  474.                     return TaskRunners<object, Task>.RunTask(task, successor)
  475.                                                     .FastUnwrap();
  476.             }
  477.         }
  478.  
  479.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  480.         public static Task<TResult> Then<TResult>(this Task task, Func<Task<TResult>> successor)
  481.         {
  482.             switch (task.Status)
  483.             {
  484.                 case TaskStatus.Faulted:
  485.                     return FromError<TResult>(task.Exception);
  486.  
  487.                 case TaskStatus.Canceled:
  488.                     return Canceled<TResult>();
  489.  
  490.                 case TaskStatus.RanToCompletion:
  491.                     return FromMethod(successor);
  492.  
  493.                 default:
  494.                     return TaskRunners<object, Task<TResult>>.RunTask(task, successor)
  495.                                                              .FastUnwrap();
  496.             }
  497.         }
  498.  
  499.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  500.         public static Task Then<TResult>(this Task<TResult> task, Action<TResult> successor)
  501.         {
  502.             switch (task.Status)
  503.             {
  504.                 case TaskStatus.Faulted:
  505.                 case TaskStatus.Canceled:
  506.                     return task;
  507.  
  508.                 case TaskStatus.RanToCompletion:
  509.                     return FromMethod(successor, task.Result);
  510.  
  511.                 default:
  512.                     return TaskRunners<TResult, object>.RunTask(task, successor);
  513.             }
  514.         }
  515.  
  516.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  517.         public static Task Then<TResult>(this Task<TResult> task, Func<TResult, Task> successor)
  518.         {
  519.             switch (task.Status)
  520.             {
  521.                 case TaskStatus.Faulted:
  522.                 case TaskStatus.Canceled:
  523.                     return task;
  524.  
  525.                 case TaskStatus.RanToCompletion:
  526.                     return FromMethod(successor, task.Result);
  527.  
  528.                 default:
  529.                     return TaskRunners<TResult, Task>.RunTask(task, t => successor(t.Result))
  530.                                                      .FastUnwrap();
  531.             }
  532.         }
  533.  
  534.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  535.         public static Task<TResult> Then<TResult, T1>(this Task<TResult> task, Func<Task<TResult>, T1, Task<TResult>> successor, T1 arg1)
  536.         {
  537.             switch (task.Status)
  538.             {
  539.                 case TaskStatus.Faulted:
  540.                 case TaskStatus.Canceled:
  541.                     return task;
  542.  
  543.                 case TaskStatus.RanToCompletion:
  544.                     return FromMethod(successor, task, arg1);
  545.  
  546.                 default:
  547.                     return GenericDelegates<TResult, Task<TResult>, T1, object, object>.ThenWithArgs(task, successor, arg1)
  548.                                                                                .FastUnwrap();
  549.             }
  550.         }
  551.  
  552.         [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions are flowed to the caller")]
  553.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  554.         public static Task Finally(this Task task, Action<object> next, object state)
  555.         {
  556.             try
  557.             {
  558.                 switch (task.Status)
  559.                 {
  560.                     case TaskStatus.Faulted:
  561.                     case TaskStatus.Canceled:
  562.                         next(state);
  563.                         return task;
  564.                     case TaskStatus.RanToCompletion:
  565.                         return FromMethod(next, state);
  566.  
  567.                     default:
  568.                         return RunTaskSynchronously(task, next, state, onlyOnSuccess: false);
  569.                 }
  570.             }
  571.             catch (Exception ex)
  572.             {
  573.                 return FromError(ex);
  574.             }
  575.         }
  576.  
  577.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  578.         public static Task RunSynchronously(this Task task, Action successor)
  579.         {
  580.             switch (task.Status)
  581.             {
  582.                 case TaskStatus.Faulted:
  583.                 case TaskStatus.Canceled:
  584.                     return task;
  585.  
  586.                 case TaskStatus.RanToCompletion:
  587.                     return FromMethod(successor);
  588.  
  589.                 default:
  590.                     return RunTaskSynchronously(task, state => ((Action)state).Invoke(), successor);
  591.             }
  592.         }
  593.  
  594.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  595.         public static Task FastUnwrap(this Task<Task> task)
  596.         {
  597.             var innerTask = (task.Status == TaskStatus.RanToCompletion) ? task.Result : null;
  598.             return innerTask ?? task.Unwrap();
  599.         }
  600.  
  601.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  602.         public static Task<T> FastUnwrap<T>(this Task<Task<T>> task)
  603.         {
  604.             var innerTask = (task.Status == TaskStatus.RanToCompletion) ? task.Result : null;
  605.             return innerTask ?? task.Unwrap();
  606.         }
  607.  
  608.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  609.         public static Task Delay(TimeSpan timeOut)
  610.         {
  611. #if NETFX_CORE
  612.             return Task.Delay(timeOut);
  613. #else
  614.             var tcs = new TaskCompletionSource<object>();
  615.  
  616.             var timer = new Timer(tcs.SetResult,
  617.             null,
  618.             timeOut,
  619.             TimeSpan.FromMilliseconds(-1));
  620.  
  621.             return tcs.Task.ContinueWithPreservedCulture(_ =>
  622.             {
  623.                 timer.Dispose();
  624.             },
  625.             TaskContinuationOptions.ExecuteSynchronously);
  626. #endif
  627.         }
  628.  
  629.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  630.         [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions are set in a tcs")]
  631.         public static Task FromMethod(Action func)
  632.         {
  633.             try
  634.             {
  635.                 func();
  636.                 return Empty;
  637.             }
  638.             catch (Exception ex)
  639.             {
  640.                 return FromError(ex);
  641.             }
  642.         }
  643.  
  644.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  645.         [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions are set in a tcs")]
  646.         public static Task FromMethod<T1>(Action<T1> func, T1 arg)
  647.         {
  648.             try
  649.             {
  650.                 func(arg);
  651.                 return Empty;
  652.             }
  653.             catch (Exception ex)
  654.             {
  655.                 return FromError(ex);
  656.             }
  657.         }
  658.  
  659.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  660.         [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions are set in a tcs")]
  661.         public static Task FromMethod<T1, T2>(Action<T1, T2> func, T1 arg1, T2 arg2)
  662.         {
  663.             try
  664.             {
  665.                 func(arg1, arg2);
  666.                 return Empty;
  667.             }
  668.             catch (Exception ex)
  669.             {
  670.                 return FromError(ex);
  671.             }
  672.         }
  673.  
  674.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  675.         [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions are set in a tcs")]
  676.         public static Task FromMethod(Func<Task> func)
  677.         {
  678.             try
  679.             {
  680.                 return func();
  681.             }
  682.             catch (Exception ex)
  683.             {
  684.                 return FromError(ex);
  685.             }
  686.         }
  687.  
  688.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  689.         [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions are set in a tcs")]
  690.         public static Task<TResult> FromMethod<TResult>(Func<Task<TResult>> func)
  691.         {
  692.             try
  693.             {
  694.                 return func();
  695.             }
  696.             catch (Exception ex)
  697.             {
  698.                 return FromError<TResult>(ex);
  699.             }
  700.         }
  701.  
  702.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  703.         [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions are set in a tcs")]
  704.         public static Task<TResult> FromMethod<TResult>(Func<TResult> func)
  705.         {
  706.             try
  707.             {
  708.                 return FromResult<TResult>(func());
  709.             }
  710.             catch (Exception ex)
  711.             {
  712.                 return FromError<TResult>(ex);
  713.             }
  714.         }
  715.  
  716.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  717.         [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions are set in a tcs")]
  718.         public static Task FromMethod<T1>(Func<T1, Task> func, T1 arg)
  719.         {
  720.             try
  721.             {
  722.                 return func(arg);
  723.             }
  724.             catch (Exception ex)
  725.             {
  726.                 return FromError(ex);
  727.             }
  728.         }
  729.  
  730.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  731.         [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions are set in a tcs")]
  732.         public static Task FromMethod<T1, T2>(Func<T1, T2, Task> func, T1 arg1, T2 arg2)
  733.         {
  734.             try
  735.             {
  736.                 return func(arg1, arg2);
  737.             }
  738.             catch (Exception ex)
  739.             {
  740.                 return FromError(ex);
  741.             }
  742.         }
  743.  
  744.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  745.         [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions are set in a tcs")]
  746.         public static Task FromMethod<T1, T2, T3>(Func<T1, T2, T3, Task> func, T1 arg1, T2 arg2, T3 arg3)
  747.         {
  748.             try
  749.             {
  750.                 return func(arg1, arg2, arg3);
  751.             }
  752.             catch (Exception ex)
  753.             {
  754.                 return FromError(ex);
  755.             }
  756.         }
  757.  
  758.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  759.         [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions are set in a tcs")]
  760.         public static Task<TResult> FromMethod<T1, TResult>(Func<T1, Task<TResult>> func, T1 arg)
  761.         {
  762.             try
  763.             {
  764.                 return func(arg);
  765.             }
  766.             catch (Exception ex)
  767.             {
  768.                 return FromError<TResult>(ex);
  769.             }
  770.         }
  771.  
  772.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  773.         [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions are set in a tcs")]
  774.         public static Task<TResult> FromMethod<T1, TResult>(Func<T1, TResult> func, T1 arg)
  775.         {
  776.             try
  777.             {
  778.                 return FromResult<TResult>(func(arg));
  779.             }
  780.             catch (Exception ex)
  781.             {
  782.                 return FromError<TResult>(ex);
  783.             }
  784.         }
  785.  
  786.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  787.         [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions are set in a tcs")]
  788.         public static Task<TResult> FromMethod<T1, T2, TResult>(Func<T1, T2, Task<TResult>> func, T1 arg1, T2 arg2)
  789.         {
  790.             try
  791.             {
  792.                 return func(arg1, arg2);
  793.             }
  794.             catch (Exception ex)
  795.             {
  796.                 return FromError<TResult>(ex);
  797.             }
  798.         }
  799.  
  800.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  801.         [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions are set in a tcs")]
  802.         public static Task<TResult> FromMethod<T1, T2, TResult>(Func<T1, T2, TResult> func, T1 arg1, T2 arg2)
  803.         {
  804.             try
  805.             {
  806.                 return FromResult<TResult>(func(arg1, arg2));
  807.             }
  808.             catch (Exception ex)
  809.             {
  810.                 return FromError<TResult>(ex);
  811.             }
  812.         }
  813.  
  814.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  815.         public static Task<T> FromResult<T>(T value)
  816.         {
  817.             var tcs = new TaskCompletionSource<T>();
  818.             tcs.SetResult(value);
  819.             return tcs.Task;
  820.         }
  821.  
  822.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  823.         internal static Task FromError(Exception e)
  824.         {
  825.             return FromError<object>(e);
  826.         }
  827.  
  828.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  829.         internal static Task<T> FromError<T>(Exception e)
  830.         {
  831.             var tcs = new TaskCompletionSource<T>();
  832.             tcs.SetUnwrappedException<T>(e);
  833.             return tcs.Task;
  834.         }
  835.  
  836.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  837.         internal static void SetUnwrappedException<T>(this TaskCompletionSource<T> tcs, Exception e)
  838.         {
  839.             var aggregateException = e as AggregateException;
  840.             if (aggregateException != null)
  841.             {
  842.                 tcs.SetException(aggregateException.InnerExceptions);
  843.             }
  844.             else
  845.             {
  846.                 tcs.SetException(e);
  847.             }
  848.         }
  849.  
  850.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  851.         internal static bool TrySetUnwrappedException<T>(this TaskCompletionSource<T> tcs, Exception e)
  852.         {
  853.             var aggregateException = e as AggregateException;
  854.             if (aggregateException != null)
  855.             {
  856.                 return tcs.TrySetException(aggregateException.InnerExceptions);
  857.             }
  858.             else
  859.             {
  860.                 return tcs.TrySetException(e);
  861.             }
  862.         }
  863.  
  864.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  865.         private static Task Canceled()
  866.         {
  867.             var tcs = new TaskCompletionSource<object>();
  868.             tcs.SetCanceled();
  869.             return tcs.Task;
  870.         }
  871.  
  872.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  873.         private static Task<T> Canceled<T>()
  874.         {
  875.             var tcs = new TaskCompletionSource<T>();
  876.             tcs.SetCanceled();
  877.             return tcs.Task;
  878.         }
  879.  
  880.         internal static Task ContinueWithPreservedCulture(this Task task, Action<Task> continuationAction, TaskContinuationOptions continuationOptions)
  881.         {
  882. #if NETFX_CORE
  883.             // The Thread class is not available on WinRT
  884.             return task.ContinueWith(continuationAction, continuationOptions);
  885. #else
  886.             var preservedCulture = Thread.CurrentThread.CurrentCulture;
  887.             var preservedUICulture = Thread.CurrentThread.CurrentUICulture;
  888.             return task.ContinueWith(t =>
  889.             {
  890.                 var replacedCulture = Thread.CurrentThread.CurrentCulture;
  891.                 var replacedUICulture = Thread.CurrentThread.CurrentUICulture;
  892.                 try
  893.                 {
  894.                     Thread.CurrentThread.CurrentCulture = preservedCulture;
  895.                     Thread.CurrentThread.CurrentUICulture = preservedUICulture;
  896.                     continuationAction(t);
  897.                 }
  898.                 finally
  899.                 {
  900.                     Thread.CurrentThread.CurrentCulture = replacedCulture;
  901.                     Thread.CurrentThread.CurrentUICulture = replacedUICulture;
  902.                 }
  903.             }, continuationOptions);
  904. #endif
  905.         }
  906.  
  907.         internal static Task ContinueWithPreservedCulture<T>(this Task<T> task, Action<Task<T>> continuationAction, TaskContinuationOptions continuationOptions)
  908.         {
  909. #if NETFX_CORE
  910.             // The Thread class is not available on WinRT
  911.             return task.ContinueWith(continuationAction, continuationOptions);
  912. #else
  913.             var preservedCulture = Thread.CurrentThread.CurrentCulture;
  914.             var preservedUICulture = Thread.CurrentThread.CurrentUICulture;
  915.             return task.ContinueWith(t =>
  916.             {
  917.                 var replacedCulture = Thread.CurrentThread.CurrentCulture;
  918.                 var replacedUICulture = Thread.CurrentThread.CurrentUICulture;
  919.                 try
  920.                 {
  921.                     Thread.CurrentThread.CurrentCulture = preservedCulture;
  922.                     Thread.CurrentThread.CurrentUICulture = preservedUICulture;
  923.                     continuationAction(t);
  924.                 }
  925.                 finally
  926.                 {
  927.                     Thread.CurrentThread.CurrentCulture = replacedCulture;
  928.                     Thread.CurrentThread.CurrentUICulture = replacedUICulture;
  929.                 }
  930.             }, continuationOptions);
  931. #endif
  932.         }
  933.  
  934.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  935.         internal static Task<TResult> ContinueWithPreservedCulture<T, TResult>(this Task<T> task, Func<Task<T>, TResult> continuationAction, TaskContinuationOptions continuationOptions)
  936.         {
  937. #if NETFX_CORE
  938.             // The Thread class is not available on WinRT
  939.             return task.ContinueWith(continuationAction, continuationOptions);
  940. #else
  941.             var preservedCulture = Thread.CurrentThread.CurrentCulture;
  942.             var preservedUICulture = Thread.CurrentThread.CurrentUICulture;
  943.             return task.ContinueWith(t =>
  944.             {
  945.                 var replacedCulture = Thread.CurrentThread.CurrentCulture;
  946.                 var replacedUICulture = Thread.CurrentThread.CurrentUICulture;
  947.                 try
  948.                 {
  949.                     Thread.CurrentThread.CurrentCulture = preservedCulture;
  950.                     Thread.CurrentThread.CurrentUICulture = preservedUICulture;
  951.                     return continuationAction(t);
  952.                 }
  953.                 finally
  954.                 {
  955.                     Thread.CurrentThread.CurrentCulture = replacedCulture;
  956.                     Thread.CurrentThread.CurrentUICulture = replacedUICulture;
  957.                 }
  958.             }, continuationOptions);
  959. #endif
  960.         }
  961.  
  962.         internal static Task ContinueWithPreservedCulture(this Task task, Action<Task> continuationAction)
  963.         {
  964.             return task.ContinueWithPreservedCulture(continuationAction, TaskContinuationOptions.None);
  965.         }
  966.  
  967.         internal static Task ContinueWithPreservedCulture<T>(this Task<T> task, Action<Task<T>> continuationAction)
  968.         {
  969.             return task.ContinueWithPreservedCulture(continuationAction, TaskContinuationOptions.None);
  970.         }
  971.  
  972.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  973.         internal static Task<TResult> ContinueWithPreservedCulture<T, TResult>(this Task<T> task, Func<Task<T>, TResult> continuationAction)
  974.         {
  975.             return task.ContinueWithPreservedCulture(continuationAction, TaskContinuationOptions.None);
  976.         }
  977.  
  978.         [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions are set in a tcs")]
  979.         private static Task RunTask(Task task, Action successor)
  980.         {
  981.             var tcs = new TaskCompletionSource<object>();
  982.             task.ContinueWithPreservedCulture(t =>
  983.             {
  984.                 if (t.IsFaulted)
  985.                 {
  986.                     tcs.SetUnwrappedException(t.Exception);
  987.                 }
  988.                 else if (t.IsCanceled)
  989.                 {
  990.                     tcs.SetCanceled();
  991.                 }
  992.                 else
  993.                 {
  994.                     try
  995.                     {
  996.                         successor();
  997.                         tcs.SetResult(null);
  998.                     }
  999.                     catch (Exception ex)
  1000.                     {
  1001.                         tcs.SetUnwrappedException(ex);
  1002.                     }
  1003.                 }
  1004.             });
  1005.  
  1006.             return tcs.Task;
  1007.         }
  1008.  
  1009.         [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions are set in a tcs")]
  1010.         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This is a shared file")]
  1011.         private static Task RunTaskSynchronously(Task task, Action<object> next, object state, bool onlyOnSuccess = true)
  1012.         {
  1013.             var tcs = new TaskCompletionSource<object>();
  1014.             task.ContinueWithPreservedCulture(t =>
  1015.             {
  1016.                 try
  1017.                 {
  1018.                     if (t.IsFaulted)
  1019.                     {
  1020.                         if (!onlyOnSuccess)
  1021.                         {
  1022.                             next(state);
  1023.                         }
  1024.  
  1025.                         tcs.SetUnwrappedException(t.Exception);
  1026.                     }
  1027.                     else if (t.IsCanceled)
  1028.                     {
  1029.                         if (!onlyOnSuccess)
  1030.                         {
  1031.                             next(state);
  1032.                         }
  1033.  
  1034.                         tcs.SetCanceled();
  1035.                     }
  1036.                     else
  1037.                     {
  1038.                         next(state);
  1039.                         tcs.SetResult(null);
  1040.                     }
  1041.                 }
  1042.                 catch (Exception ex)
  1043.                 {
  1044.                     tcs.SetUnwrappedException(ex);
  1045.                 }
  1046.             },
  1047.             TaskContinuationOptions.ExecuteSynchronously);
  1048.  
  1049.             return tcs.Task;
  1050.         }
  1051.  
  1052.         private static class TaskRunners<T, TResult>
  1053.         {
  1054.             [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions are set in a tcs")]
  1055.             internal static Task RunTask(Task<T> task, Action<T> successor)
  1056.             {
  1057.                 var tcs = new TaskCompletionSource<object>();
  1058.                 task.ContinueWithPreservedCulture(t =>
  1059.                 {
  1060.                     if (t.IsFaulted)
  1061.                     {
  1062.                         tcs.SetUnwrappedException(t.Exception);
  1063.                     }
  1064.                     else if (t.IsCanceled)
  1065.                     {
  1066.                         tcs.SetCanceled();
  1067.                     }
  1068.                     else
  1069.                     {
  1070.                         try
  1071.                         {
  1072.                             successor(t.Result);
  1073.                             tcs.SetResult(null);
  1074.                         }
  1075.                         catch (Exception ex)
  1076.                         {
  1077.                             tcs.SetUnwrappedException(ex);
  1078.                         }
  1079.                     }
  1080.                 });
  1081.  
  1082.                 return tcs.Task;
  1083.             }
  1084.  
  1085.             [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions are set in a tcs")]
  1086.             internal static Task<TResult> RunTask(Task task, Func<TResult> successor)
  1087.             {
  1088.                 var tcs = new TaskCompletionSource<TResult>();
  1089.                 task.ContinueWithPreservedCulture(t =>
  1090.                 {
  1091.                     if (t.IsFaulted)
  1092.                     {
  1093.                         tcs.SetUnwrappedException(t.Exception);
  1094.                     }
  1095.                     else if (t.IsCanceled)
  1096.                     {
  1097.                         tcs.SetCanceled();
  1098.                     }
  1099.                     else
  1100.                     {
  1101.                         try
  1102.                         {
  1103.                             tcs.SetResult(successor());
  1104.                         }
  1105.                         catch (Exception ex)
  1106.                         {
  1107.                             tcs.SetUnwrappedException(ex);
  1108.                         }
  1109.                     }
  1110.                 });
  1111.  
  1112.                 return tcs.Task;
  1113.             }
  1114.  
  1115.             [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions are set in a tcs")]
  1116.             internal static Task<TResult> RunTask(Task<T> task, Func<Task<T>, TResult> successor)
  1117.             {
  1118.                 var tcs = new TaskCompletionSource<TResult>();
  1119.                 task.ContinueWithPreservedCulture(t =>
  1120.                 {
  1121.                     if (task.IsFaulted)
  1122.                     {
  1123.                         tcs.SetUnwrappedException(t.Exception);
  1124.                     }
  1125.                     else if (task.IsCanceled)
  1126.                     {
  1127.                         tcs.SetCanceled();
  1128.                     }
  1129.                     else
  1130.                     {
  1131.                         try
  1132.                         {
  1133.                             tcs.SetResult(successor(t));
  1134.                         }
  1135.                         catch (Exception ex)
  1136.                         {
  1137.                             tcs.SetUnwrappedException(ex);
  1138.                         }
  1139.                     }
  1140.                 });
  1141.  
  1142.                 return tcs.Task;
  1143.             }
  1144.         }
  1145.  
  1146.         private static class GenericDelegates<T, TResult, T1, T2, T3>
  1147.         {
  1148.             internal static Task ThenWithArgs(Task task, Action<T1> successor, T1 arg1)
  1149.             {
  1150.                 return RunTask(task, () => successor(arg1));
  1151.             }
  1152.  
  1153.             internal static Task ThenWithArgs(Task task, Action<T1, T2> successor, T1 arg1, T2 arg2)
  1154.             {
  1155.                 return RunTask(task, () => successor(arg1, arg2));
  1156.             }
  1157.  
  1158.             internal static Task<TResult> ThenWithArgs(Task task, Func<T1, TResult> successor, T1 arg1)
  1159.             {
  1160.                 return TaskRunners<object, TResult>.RunTask(task, () => successor(arg1));
  1161.             }
  1162.  
  1163.             internal static Task<TResult> ThenWithArgs(Task task, Func<T1, T2, TResult> successor, T1 arg1, T2 arg2)
  1164.             {
  1165.                 return TaskRunners<object, TResult>.RunTask(task, () => successor(arg1, arg2));
  1166.             }
  1167.  
  1168.             internal static Task<TResult> ThenWithArgs(Task<T> task, Func<T, T1, TResult> successor, T1 arg1)
  1169.             {
  1170.                 return TaskRunners<T, TResult>.RunTask(task, t => successor(t.Result, arg1));
  1171.             }
  1172.  
  1173.             internal static Task<Task> ThenWithArgs(Task task, Func<T1, Task> successor, T1 arg1)
  1174.             {
  1175.                 return TaskRunners<object, Task>.RunTask(task, () => successor(arg1));
  1176.             }
  1177.  
  1178.             internal static Task<Task> ThenWithArgs(Task task, Func<T1, T2, Task> successor, T1 arg1, T2 arg2)
  1179.             {
  1180.                 return TaskRunners<object, Task>.RunTask(task, () => successor(arg1, arg2));
  1181.             }
  1182.  
  1183.             internal static Task<Task> ThenWithArgs(Task task, Func<T1, T2, T3, Task> successor, T1 arg1, T2 arg2, T3 arg3)
  1184.             {
  1185.                 return TaskRunners<object, Task>.RunTask(task, () => successor(arg1, arg2, arg3));
  1186.             }
  1187.  
  1188.             internal static Task<Task<TResult>> ThenWithArgs(Task<T> task, Func<T, T1, Task<TResult>> successor, T1 arg1)
  1189.             {
  1190.                 return TaskRunners<T, Task<TResult>>.RunTask(task, t => successor(t.Result, arg1));
  1191.             }
  1192.  
  1193.             internal static Task<Task<T>> ThenWithArgs(Task<T> task, Func<Task<T>, T1, Task<T>> successor, T1 arg1)
  1194.             {
  1195.                 return TaskRunners<T, Task<T>>.RunTask(task, t => successor(t, arg1));
  1196.             }
  1197.         }
  1198.  
  1199.         private static class TaskCache<T>
  1200.         {
  1201.             public static Task<T> Empty = MakeTask<T>(default(T));
  1202.         }
  1203.     }
  1204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement