Class ThreadingExtensions

Convenience extensions for utilizing multiple threads and using the ThreadingHelper.

Inheritance
System.Object ThreadingExtensions
Inherited Members
System.Object.ToString() System.Object.Equals(System.Object) System.Object.Equals(System.Object, System.Object) System.Object.ReferenceEquals(System.Object, System.Object) System.Object.GetHashCode() System.Object.GetType() System.Object.MemberwiseClone()
Namespace: BepInEx.Unity.Mono
Assembly: BepInEx.Unity.Mono.dll
Syntax
public static class ThreadingExtensions

Methods

ForEachParallel<T>(IList<T>, Action<T>, Int32)

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.

RunParallel<TIn, TOut>(IEnumerable<TIn>, Func<TIn, TOut>, Int32)

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.

RunParallel<TIn, TOut>(IList<TIn>, Func<TIn, TOut>, Int32)

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.