Don't like ads? PRO users don't see any ads ;-)

Tasks

By: RichardD on Oct 4th, 2011  |  syntax: C#  |  size: 2.47 KB  |  hits: 29  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public static class CompletedTask
  2. {
  3.     public static Task<TResult> Create<TResult>(TResult result)
  4.     {
  5.         var tcs = new TaskCompletionSource<TResult>();
  6.         tcs.TrySetResult(result);
  7.         return tcs.Task;
  8.     }
  9. }
  10.  
  11. public static class TaskExtensions
  12. {
  13.     public static bool TrySetFromTask<TResult>(this TaskCompletionSource<TResult> resultSetter, Task task)
  14.     {
  15.         if (null == resultSetter) throw new ArgumentNullException("resultSetter");
  16.         if (null == task) throw new ArgumentNullException("task");
  17.  
  18.         switch (task.Status)
  19.         {
  20.             case TaskStatus.RanToCompletion:
  21.             {
  22.                 var taskResult = task as Task<TResult>;
  23.                 TResult result = (null == taskResult) ? default(TResult) : taskResult.Result;
  24.                 return resultSetter.TrySetResult(result);
  25.             }
  26.             case TaskStatus.Faulted:
  27.             {
  28.                 var error = task.Exception;
  29.                 var errors = (null == error) ? Enumerable.Empty<Exception>() : error.InnerExceptions;
  30.                 return resultSetter.TrySetException(errors);
  31.             }
  32.             case TaskStatus.Canceled:
  33.             {
  34.                 return resultSetter.TrySetCanceled();
  35.             }
  36.             default:
  37.             {
  38.                 throw new InvalidOperationException();
  39.             }
  40.         }
  41.     }
  42.  
  43.     public static Task<TResult> ToApm<TResult>(this Task<TResult> task, AsyncCallback callback, object state)
  44.     {
  45.         if (null == task) throw new ArgumentNullException("task");
  46.  
  47.         if (task.AsyncState == state)
  48.         {
  49.             if (null != callback)
  50.             {
  51.                 task.ContinueWith(t => callback(t),
  52.                     CancellationToken.None,
  53.                     TaskContinuationOptions.ExecuteSynchronously,
  54.                     TaskScheduler.Default);
  55.             }
  56.  
  57.             return task;
  58.         }
  59.  
  60.         var tcs = new TaskCompletionSource<TResult>(state);
  61.        
  62.         task.ContinueWith(t => tcs.TrySetFromTask(t),
  63.             CancellationToken.None,
  64.             TaskContinuationOptions.ExecuteSynchronously,
  65.             TaskScheduler.Default);
  66.        
  67.         if (null != callback)
  68.         {
  69.             tcs.Task.ContinueWith(t => callback(t),
  70.                 CancellationToken.None,
  71.                 TaskContinuationOptions.ExecuteSynchronously,
  72.                 TaskScheduler.Default);
  73.         }
  74.        
  75.         return tcs.Task;
  76.     }
  77. }
  78.