예제 #1
0
    /// <summary>
    /// Executes the given work items potentially in parallel with each other.
    /// This method will block until all work is completed.
    /// </summary>
    /// <param name="actions">The work to execute.</param>
    /// <exception cref="ArgumentNullException">
    /// One of the parameters is <see langword="null"/>.
    /// </exception>
    public static void Do(params Action[] actions)
    {

      if (actions == null)
        throw new ArgumentNullException("actions");

      List<Task> tasks = TaskListPool.Obtain();

      int numberOfTasks = actions.Length;
      for (int i = 0; i < numberOfTasks; i++)
      {
        var work = DelegateWork.Create();
        work.Action = actions[i];
        work.Options = WorkOptions.Default;
        tasks.Add(Start(work));
      }

      for (int i = 0; i < numberOfTasks; i++)
        tasks[i].Wait();

      TaskListPool.Recycle(tasks);
#else
      throw new NotSupportedException("Unity builds do not yet support multithreading.");

    }
예제 #2
0
    /// <summary>
    /// Starts a task in a secondary worker thread. Intended for long running, blocking, work
    /// such as I/O.
    /// </summary>
    /// <param name="action">The work to execute.</param>
    /// <param name="completionCallback">
    /// A method which will be called in <see cref="RunCallbacks"/> once this task has completed.
    /// </param>
    /// <returns>A task which represents the asynchronous operation.</returns>
    /// <remarks>
    /// <strong>Important:</strong> The completion callback is not executed automatically. Instead, 
    /// the callback is only executed when <see cref="RunCallbacks"/> is called. See 
    /// <see cref="RunCallbacks"/> for additional information.
    /// </remarks>
    /// <exception cref="ArgumentNullException">
    /// <paramref name="action"/> is <see langword="null"/>.
    /// </exception>
    public static Task StartBackground(Action action, Action completionCallback)
    {
      if (action == null)
        throw new ArgumentNullException("action");

      var work = DelegateWork.Create();
      work.Action = action;
      work.Options = WorkOptions.Default;
      return StartBackground(work, completionCallback);
    }
예제 #3
0
    /// <summary>
    /// Creates and starts a task to execute the given work.
    /// </summary>
    /// <param name="action">The work to execute in parallel.</param>
    /// <param name="options">The work options to use with this action.</param>
    /// <param name="completionCallback">
    /// A method which will be called in <see cref="RunCallbacks"/> once this task has completed.
    /// </param>
    /// <returns>A task which represents the asynchronous operation.</returns>
    /// <remarks>
    /// <strong>Important:</strong> The completion callback is not executed automatically. Instead, 
    /// the callback is only executed when <see cref="RunCallbacks"/> is called. See 
    /// <see cref="RunCallbacks"/> for additional information.
    /// </remarks>
    /// <exception cref="ArgumentException">
    /// Invalid number of maximum threads set in <see cref="IWork.Options"/>.
    /// </exception>
    /// <exception cref="ArgumentNullException">
    /// <paramref name="action"/> is <see langword="null"/>.
    /// </exception>
    public static Task Start(Action action, WorkOptions options, Action completionCallback)
    {

      if (options.MaximumThreads < 1)
        throw new ArgumentException("options.MaximumThreads cannot be less than 1.", "options");

      var work = DelegateWork.Create();
      work.Action = action;
      work.Options = options;
      return Start(work, completionCallback);
#else
      throw new NotSupportedException("Unity builds do not yet support multithreading.");

    }
예제 #4
0
    /// <overloads>
    /// <summary>
    /// Executes the given work items potentially in parallel with each other.
    /// This method will block until all work is completed.
    /// </summary>
    /// </overloads>
    /// 
    /// <summary>
    /// Executes the given work items potentially in parallel with each other.
    /// This method will block until all work is completed.
    /// </summary>
    /// <param name="action1">The first piece of work to execute.</param>
    /// <param name="action2">The second piece of work to execute.</param>
    /// <exception cref="ArgumentNullException">
    /// Either <paramref name="action1"/> or <paramref name="action2"/> is <see langword="null"/>.
    /// </exception>
    public static void Do(Action action1, Action action2)
    {

      if (action1 == null)
        throw new ArgumentNullException("action1");
      if (action2 == null)
        throw new ArgumentNullException("action2");

      var work = DelegateWork.Create();
      work.Action = action2;
      work.Options = WorkOptions.Default;
      var task = Start(work);
      action1();
      task.Wait();
#else
      throw new NotSupportedException("Unity builds do not yet support multithreading.");

    }