/// <summary>Initializes a new <see cref="WorkItemTask"/> with the work to be done by that task, and an optional /// <see cref="TaskCancellationEvent"/> allowing the task to be canceled along with other tasks that share the same /// cancellation event. /// </summary> public WorkItemTask(TaskDelegate work, TaskCancellationEvent cancellationEvent) : base(cancellationEvent) { if (work == null) { throw new ArgumentNullException(); } this.work = work; }
/// <summary>Runs a number of instances of a work item simultaneously, and returns the results from all instances when they've /// completed or been canceled using the given cancellation event. /// </summary> /// <include file="documentation.xml" path="/Utilities/Tasks/ParallelismRemarks/*"/> public static T[] Parallelize <T>(TaskDelegate <T> work, int instanceCount, TaskCancellationEvent cancellationEvent) { using (CompositeTask task = CreateParallel(work, instanceCount, cancellationEvent)) { task.Run(); return(task.GetResults <T>()); } }
/// <summary>Creates and returns a task that represents the running of a number of instances of a work item simultaneously. /// The method accepts an optional cancellation event that can be associated with all instances of the work item, as well as /// the aggregate task that encapsulates them. /// </summary> /// <include file="documentation.xml" path="/Utilities/Tasks/ParallelismRemarks/*"/> public static Task CreateParallel(TaskDelegate work, int instanceCount, TaskCancellationEvent cancellationEvent) { if (instanceCount <= 0) { throw new ArgumentOutOfRangeException(); } if (instanceCount == 1) { return(new WorkItemTask(work, cancellationEvent)); } else { Task[] tasks = new Task[instanceCount]; for (int i = 0; i < tasks.Length; i++) { tasks[i] = new WorkItemTask(work, cancellationEvent); } return(new CompositeTask(tasks, cancellationEvent)); } }
/// <summary>Initializes a new <see cref="CompositeTask"/> with the set of other tasks to perform and an optional /// <see cref="TaskCancellationEvent"/> allowing the task to be canceled along with other tasks that share the same /// <see cref="TaskCancellationEvent"/>. /// </summary> public CompositeTask(IEnumerable <Task> tasks, TaskCancellationEvent cancellationEvent) : base(cancellationEvent) { if (tasks == null) { throw new ArgumentNullException(); } List <Task> taskList = new List <Task>(); foreach (Task task in tasks) { if (task == null) { throw new ArgumentException("A task was null."); } task.Completed += task_Completed; taskList.Add(task); } this.tasks = taskList.ToArray(); }
/// <summary>Initializes a new <see cref="Task"/> with an optional <see cref="TaskCancellationEvent"/> allowing the task to be /// canceled along with other tasks that share the same cancellation event. /// </summary> protected Task(TaskCancellationEvent cancellationEvent) { this.cancellationEvent = cancellationEvent; }
/// <summary>Runs an instance of a work item on each hardware thread, and returns the results from all instances when they've /// completed or been canceled using the given cancellation event. /// </summary> /// <include file="documentation.xml" path="/Utilities/Tasks/ParallelismRemarks/*"/> public static T[] Parallelize <T>(TaskDelegate <T> work, TaskCancellationEvent cancellationEvent) { return(Parallelize(work, SystemInformation.GetAvailableCpuThreads(), cancellationEvent)); }
/// <summary>Runs a number of instances of a work item simultaneously, and returns when all instances have completed or been /// canceled using the given cancellation event. /// </summary> /// <include file="documentation.xml" path="/Utilities/Tasks/ParallelismRemarks/*"/> public static void Parallelize(TaskDelegate work, int instanceCount, TaskCancellationEvent cancellationEvent) { using (Task task = CreateParallel(work, instanceCount, cancellationEvent)) task.Run(); }
/// <summary>Runs an instance of a work item on each hardware thread, and returns when all instances have completed or been /// canceled using the given cancellation event. /// </summary> /// <include file="documentation.xml" path="/Utilities/Tasks/ParallelismRemarks/*"/> public static void Parallelize(TaskDelegate work, TaskCancellationEvent cancellationEvent) { Parallelize(work, SystemInformation.GetAvailableCpuThreads(), cancellationEvent); }
/// <summary>Creates and returns a task that represents the running of an instance of a work item on each hardware thread. The /// method accepts an optional cancellation event that can be associated with all instances of the work item, as well as the /// aggregate task that encapsulates them. /// </summary> /// <include file="documentation.xml" path="/Utilities/Tasks/ParallelismRemarks/*"/> public static CompositeTask CreateParallel <T>(TaskDelegate <T> work, TaskCancellationEvent cancellationEvent) { return(CreateParallel(work, SystemInformation.GetAvailableCpuThreads(), cancellationEvent)); }