Description
The ForEach
method in the Sandbox.Utility.Parallel
class provides a mechanism to execute a specified action on each element of a collection in parallel. This method is useful for improving performance by utilizing multiple threads to process elements concurrently.
Usage
To use the ForEach
method, pass in an IEnumerable<T>
as the source
parameter, which represents the collection of elements to be processed. The body
parameter is an Action<T>
delegate that defines the operation to perform on each element.
The method returns a Boolean
indicating whether the operation completed successfully.
Example
// Example usage of Sandbox.Utility.Parallel.ForEach
var numbers = new List<int> { 1, 2, 3, 4, 5 };
bool result = Parallel.ForEach(numbers, number =>
{
// Perform some operation on each number
Console.WriteLine($"Processing number: {number}");
});
if (result)
{
Console.WriteLine("All operations completed successfully.");
}
else
{
Console.WriteLine("An error occurred during processing.");
}