Class ThreadingExtensions
Convenience extensions for utilizing multiple threads and using the ThreadingHelper.
Syntax
public static class ThreadingExtensions
Methods
Apply a function to a collection of data by spreading the work on multiple threads. Lower overhead than RunParallel but it blocks the main thread until all work is completed or an exception has been thrown.
Declaration
public static void ForEachParallel<T>(this IList<T> data, Action<T> work, int workerCount = -1)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IList<T> |
data |
Input values for the work function. |
System.Action<T> |
work |
Function to apply to the data on multiple threads at once. |
System.Int32 |
workerCount |
Number of worker threads. By default Environment.ProcessorCount is used. |
Type Parameters
Name | Description |
---|---|
T |
Type of the input values. |
Exceptions
Type | Condition |
---|---|
System.Reflection.TargetInvocationException |
An exception was thrown inside one of the threads, and the operation was aborted. |
System.ArgumentException |
Need at least 1 workerCount. |
Apply a function to a collection of data by spreading the work on multiple threads. Outputs of the functions are returned to the current thread and yielded one by one.
Declaration
public static IEnumerable<TOut> RunParallel<TIn, TOut>(this IEnumerable<TIn> data, Func<TIn, TOut> work, int workerCount = -1)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<TIn> |
data |
|
System.Func<TIn, TOut> |
work |
Function to apply to the data on multiple threads at once. |
System.Int32 |
workerCount |
Number of worker threads. By default Environment.ProcessorCount is used. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<TOut> |
Type Parameters
Name | Description |
---|---|
TIn |
Type of the input values. |
TOut |
Type of the output values. |
Exceptions
Type | Condition |
---|---|
System.Reflection.TargetInvocationException |
An exception was thrown inside one of the threads, and the operation was aborted. |
System.ArgumentException |
Need at least 1 workerCount. |
Apply a function to a collection of data by spreading the work on multiple threads. Outputs of the functions are returned to the current thread and yielded one by one.
Declaration
public static IEnumerable<TOut> RunParallel<TIn, TOut>(this IList<TIn> data, Func<TIn, TOut> work, int workerCount = -1)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IList<TIn> |
data |
Input values for the work function. |
System.Func<TIn, TOut> |
work |
Function to apply to the data on multiple threads at once. |
System.Int32 |
workerCount |
Number of worker threads. By default Environment.ProcessorCount is used. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<TOut> |
Type Parameters
Name | Description |
---|---|
TIn |
Type of the input values. |
TOut |
Type of the output values. |
Exceptions
Type | Condition |
---|---|
System.Reflection.TargetInvocationException |
An exception was thrown inside one of the threads, and the operation was aborted. |
System.ArgumentException |
Need at least 1 workerCount. |