Advertisement
Guest User

Task.WaitAll

a guest
Apr 6th, 2012
2,100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.47 KB | None | 0 0
  1. public static bool WaitAll(Task[] tasks, int millisecondsTimeout, CancellationToken cancellationToken)
  2. {
  3.     if (tasks == null)
  4.     {
  5.         throw new ArgumentNullException("tasks");
  6.     }
  7.     if (millisecondsTimeout < -1)
  8.     {
  9.         throw new ArgumentOutOfRangeException("millisecondsTimeout");
  10.     }
  11.     cancellationToken.ThrowIfCancellationRequested();
  12.     List<Exception> innerExceptions = null;
  13.     List<Task> list = null;
  14.     List<Task> list2 = null;
  15.     bool flag = false;
  16.     bool flag2 = false;
  17.     bool flag3 = true;
  18.     for (int i = tasks.Length - 1; i >= 0; i--)
  19.     {
  20.         Task task = tasks[i];
  21.         if (task == null)
  22.         {
  23.             throw new ArgumentException(Environment.GetResourceString("Task_WaitMulti_NullTask"), "tasks");
  24.         }
  25.         bool flag4 = task.IsCompleted;
  26.         if (!flag4)
  27.         {
  28.             if (millisecondsTimeout != -1 || cancellationToken.CanBeCanceled)
  29.             {
  30.                 Task item = task;
  31.                 int initSize = tasks.Length;
  32.                 Task.AddToList<Task>(item, ref list, initSize);
  33.             }
  34.             else
  35.             {
  36.                 flag4 = (task.WrappedTryRunInline() && task.IsCompleted);
  37.                 if (!flag4)
  38.                 {
  39.                     Task item2 = task;
  40.                     int initSize2 = tasks.Length;
  41.                     Task.AddToList<Task>(item2, ref list, initSize2);
  42.                 }
  43.             }
  44.         }
  45.         if (flag4)
  46.         {
  47.             if (task.IsFaulted)
  48.             {
  49.                 flag = true;
  50.             }
  51.             else
  52.             {
  53.                 if (task.IsCanceled)
  54.                 {
  55.                     flag2 = true;
  56.                 }
  57.             }
  58.             if (task.IsWaitNotificationEnabled)
  59.             {
  60.                 Task item3 = task;
  61.                 int initSize3 = 1;
  62.                 Task.AddToList<Task>(item3, ref list2, initSize3);
  63.             }
  64.         }
  65.     }
  66.     if (list != null)
  67.     {
  68.         flag3 = Task.WaitAllBlockingCore(list, millisecondsTimeout, cancellationToken);
  69.         if (flag3)
  70.         {
  71.             foreach (Task current in list)
  72.             {
  73.                 if (current.IsFaulted)
  74.                 {
  75.                     flag = true;
  76.                 }
  77.                 else
  78.                 {
  79.                     if (current.IsCanceled)
  80.                     {
  81.                         flag2 = true;
  82.                     }
  83.                 }
  84.                 if (current.IsWaitNotificationEnabled)
  85.                 {
  86.                     Task item4 = current;
  87.                     int initSize4 = 1;
  88.                     Task.AddToList<Task>(item4, ref list2, initSize4);
  89.                 }
  90.             }
  91.         }
  92.         GC.KeepAlive(tasks);
  93.     }
  94.     if (flag3 && list2 != null)
  95.     {
  96.         foreach (Task current2 in list2)
  97.         {
  98.             if (current2.NotifyDebuggerOfWaitCompletionIfNecessary())
  99.             {
  100.                 break;
  101.             }
  102.         }
  103.     }
  104.     if (flag3 && (flag || flag2))
  105.     {
  106.         if (!flag)
  107.         {
  108.             cancellationToken.ThrowIfCancellationRequested();
  109.         }
  110.         for (int j = 0; j < tasks.Length; j++)
  111.         {
  112.             Task t = tasks[j];
  113.             Task.AddExceptionsForCompletedTask(ref innerExceptions, t);
  114.         }
  115.         throw new AggregateException(innerExceptions);
  116.     }
  117.     return flag3;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement