Table of Contents

ParallelFactory Class

Definition

Namespace
Cuemon.Threading
Assemblies
Cuemon.Threading.dll
Source
src/Cuemon.Threading/ParallelFactory.ForResultAsync.cs

Provides a factory based way to encapsulate and re-use existing code while adding support for typically long-running parallel loops and regions.

public static class ParallelFactory
Inheritance
ParallelFactory

Examples

The following example demonstrates basic parallel loops using ParallelFactory.For and ParallelFactory.ForEach. Each iteration prints its index or item to the console, illustrating how the factory distributes work across threads.

using System;
using Cuemon.Threading;

namespace Cuemon.Threading;

public class ParallelFactoryExample
{
    public void Demonstrate()
    {
        ParallelFactory.For(0, 5, i =>
        {
            Console.WriteLine($"Processing iteration {i}");
        });

        var items = new[] { "apple", "banana", "cherry" };
        ParallelFactory.ForEach(items, item =>
        {
            Console.WriteLine($"Processing {item}");
        });
    }
}

Methods

Name Description
For(int, int, Action<int>, Action<AsyncTaskFactoryOptions>)

Executes a parallel for loop.

For(long, long, Action<long>, Action<AsyncTaskFactoryOptions>)

Executes a parallel for loop.

ForAsync(int, int, Func<int, CancellationToken, Task>, Action<AsyncWorkloadOptions>)

Executes a parallel for loop.

ForAsync(long, long, Func<long, CancellationToken, Task>, Action<AsyncWorkloadOptions>)

Executes a parallel for loop.

ForAsync<T>(int, int, Func<int, T, CancellationToken, Task>, T, Action<AsyncWorkloadOptions>)

Executes a parallel for loop.

ForAsync<T>(long, long, Func<long, T, CancellationToken, Task>, T, Action<AsyncWorkloadOptions>)

Executes a parallel for loop.

ForAsync<T1, T2>(int, int, Func<int, T1, T2, CancellationToken, Task>, T1, T2, Action<AsyncWorkloadOptions>)

Executes a parallel for loop.

ForAsync<T1, T2>(long, long, Func<long, T1, T2, CancellationToken, Task>, T1, T2, Action<AsyncWorkloadOptions>)

Executes a parallel for loop.

ForAsync<T1, T2, T3>(int, int, Func<int, T1, T2, T3, CancellationToken, Task>, T1, T2, T3, Action<AsyncWorkloadOptions>)

Executes a parallel for loop.

ForAsync<T1, T2, T3>(long, long, Func<long, T1, T2, T3, CancellationToken, Task>, T1, T2, T3, Action<AsyncWorkloadOptions>)

Executes a parallel for loop.

ForAsync<T1, T2, T3, T4>(int, int, Func<int, T1, T2, T3, T4, CancellationToken, Task>, T1, T2, T3, T4, Action<AsyncWorkloadOptions>)

Executes a parallel for loop.

ForAsync<T1, T2, T3, T4>(long, long, Func<long, T1, T2, T3, T4, CancellationToken, Task>, T1, T2, T3, T4, Action<AsyncWorkloadOptions>)

Executes a parallel for loop.

ForAsync<T1, T2, T3, T4, T5>(int, int, Func<int, T1, T2, T3, T4, T5, CancellationToken, Task>, T1, T2, T3, T4, T5, Action<AsyncWorkloadOptions>)

Executes a parallel for loop.

ForAsync<T1, T2, T3, T4, T5>(long, long, Func<long, T1, T2, T3, T4, T5, CancellationToken, Task>, T1, T2, T3, T4, T5, Action<AsyncWorkloadOptions>)

Executes a parallel for loop.

ForEachAsync<TSource>(IEnumerable<TSource>, Func<TSource, CancellationToken, Task>, Action<AsyncWorkloadOptions>)

Executes a parallel foreach loop.

ForEachAsync<TSource, T>(IEnumerable<TSource>, Func<TSource, T, CancellationToken, Task>, T, Action<AsyncWorkloadOptions>)

Executes a parallel foreach loop.

ForEachAsync<TSource, T1, T2>(IEnumerable<TSource>, Func<TSource, T1, T2, CancellationToken, Task>, T1, T2, Action<AsyncWorkloadOptions>)

Executes a parallel foreach loop.

ForEachAsync<TSource, T1, T2, T3>(IEnumerable<TSource>, Func<TSource, T1, T2, T3, CancellationToken, Task>, T1, T2, T3, Action<AsyncWorkloadOptions>)

Executes a parallel foreach loop.

ForEachAsync<TSource, T1, T2, T3, T4>(IEnumerable<TSource>, Func<TSource, T1, T2, T3, T4, CancellationToken, Task>, T1, T2, T3, T4, Action<AsyncWorkloadOptions>)

Executes a parallel foreach loop.

ForEachAsync<TSource, T1, T2, T3, T4, T5>(IEnumerable<TSource>, Func<TSource, T1, T2, T3, T4, T5, CancellationToken, Task>, T1, T2, T3, T4, T5, Action<AsyncWorkloadOptions>)

Executes a parallel foreach loop.

ForEachResultAsync<TSource, TResult>(IEnumerable<TSource>, Func<TSource, CancellationToken, Task<TResult>>, Action<AsyncWorkloadOptions>)

Executes a parallel foreach loop where the return value of the function delegate worker is stored in the same sequential order as source.

ForEachResultAsync<TSource, T, TResult>(IEnumerable<TSource>, Func<TSource, T, CancellationToken, Task<TResult>>, T, Action<AsyncWorkloadOptions>)

Executes a parallel foreach loop where the return value of the function delegate worker is stored in the same sequential order as source.

ForEachResultAsync<TSource, T1, T2, TResult>(IEnumerable<TSource>, Func<TSource, T1, T2, CancellationToken, Task<TResult>>, T1, T2, Action<AsyncWorkloadOptions>)

Executes a parallel foreach loop where the return value of the function delegate worker is stored in the same sequential order as source.

ForEachResultAsync<TSource, T1, T2, T3, TResult>(IEnumerable<TSource>, Func<TSource, T1, T2, T3, CancellationToken, Task<TResult>>, T1, T2, T3, Action<AsyncWorkloadOptions>)

Executes a parallel foreach loop where the return value of the function delegate worker is stored in the same sequential order as source.

ForEachResultAsync<TSource, T1, T2, T3, T4, TResult>(IEnumerable<TSource>, Func<TSource, T1, T2, T3, T4, CancellationToken, Task<TResult>>, T1, T2, T3, T4, Action<AsyncWorkloadOptions>)

Executes a parallel foreach loop where the return value of the function delegate worker is stored in the same sequential order as source.

ForEachResultAsync<TSource, T1, T2, T3, T4, T5, TResult>(IEnumerable<TSource>, Func<TSource, T1, T2, T3, T4, T5, CancellationToken, Task<TResult>>, T1, T2, T3, T4, T5, Action<AsyncWorkloadOptions>)

Executes a parallel foreach loop

ForEachResult<TSource, TResult>(IEnumerable<TSource>, Func<TSource, TResult>, Action<AsyncTaskFactoryOptions>)

Executes a parallel foreach loop where the return value of the function delegate worker is stored in the same sequential order as source.

ForEachResult<TSource, T, TResult>(IEnumerable<TSource>, Func<TSource, T, TResult>, T, Action<AsyncTaskFactoryOptions>)

Executes a parallel foreach loop where the return value of the function delegate worker is stored in the same sequential order as source.

ForEachResult<TSource, T1, T2, TResult>(IEnumerable<TSource>, Func<TSource, T1, T2, TResult>, T1, T2, Action<AsyncTaskFactoryOptions>)

Executes a parallel foreach loop where the return value of the function delegate worker is stored in the same sequential order as source.

ForEachResult<TSource, T1, T2, T3, TResult>(IEnumerable<TSource>, Func<TSource, T1, T2, T3, TResult>, T1, T2, T3, Action<AsyncTaskFactoryOptions>)

Executes a parallel foreach loop where the return value of the function delegate worker is stored in the same sequential order as source.

ForEachResult<TSource, T1, T2, T3, T4, TResult>(IEnumerable<TSource>, Func<TSource, T1, T2, T3, T4, TResult>, T1, T2, T3, T4, Action<AsyncTaskFactoryOptions>)

Executes a parallel foreach loop where the return value of the function delegate worker is stored in the same sequential order as source.

ForEachResult<TSource, T1, T2, T3, T4, T5, TResult>(IEnumerable<TSource>, Func<TSource, T1, T2, T3, T4, T5, TResult>, T1, T2, T3, T4, T5, Action<AsyncTaskFactoryOptions>)

Executes a parallel foreach loop

ForEach<TSource>(IEnumerable<TSource>, Action<TSource>, Action<AsyncTaskFactoryOptions>)

Executes a parallel foreach loop.

ForEach<TSource, T>(IEnumerable<TSource>, Action<TSource, T>, T, Action<AsyncTaskFactoryOptions>)

Executes a parallel foreach loop.

ForEach<TSource, T1, T2>(IEnumerable<TSource>, Action<TSource, T1, T2>, T1, T2, Action<AsyncTaskFactoryOptions>)

Executes a parallel foreach loop.

ForEach<TSource, T1, T2, T3>(IEnumerable<TSource>, Action<TSource, T1, T2, T3>, T1, T2, T3, Action<AsyncTaskFactoryOptions>)

Executes a parallel foreach loop.

ForEach<TSource, T1, T2, T3, T4>(IEnumerable<TSource>, Action<TSource, T1, T2, T3, T4>, T1, T2, T3, T4, Action<AsyncTaskFactoryOptions>)

Executes a parallel foreach loop.

ForEach<TSource, T1, T2, T3, T4, T5>(IEnumerable<TSource>, Action<TSource, T1, T2, T3, T4, T5>, T1, T2, T3, T4, T5, Action<AsyncTaskFactoryOptions>)

Executes a parallel foreach loop.

ForResultAsync<TResult>(int, int, Func<int, CancellationToken, Task<TResult>>, Action<AsyncWorkloadOptions>)

Executes a parallel for loop where the return value of the function delegate worker is stored in the same sequential order as the for loop.

ForResultAsync<TResult>(long, long, Func<long, CancellationToken, Task<TResult>>, Action<AsyncWorkloadOptions>)

Executes a parallel for loop where the return value of the function delegate worker is stored in the same sequential order as the for loop.

ForResultAsync<T, TResult>(int, int, Func<int, T, CancellationToken, Task<TResult>>, T, Action<AsyncWorkloadOptions>)

Executes a parallel for loop where the return value of the function delegate worker is stored in the same sequential order as the for loop.

ForResultAsync<T, TResult>(long, long, Func<long, T, CancellationToken, Task<TResult>>, T, Action<AsyncWorkloadOptions>)

Executes a parallel for loop where the return value of the function delegate worker is stored in the same sequential order as the for loop.

ForResultAsync<T1, T2, TResult>(int, int, Func<int, T1, T2, CancellationToken, Task<TResult>>, T1, T2, Action<AsyncWorkloadOptions>)

Executes a parallel for loop where the return value of the function delegate worker is stored in the same sequential order as the for loop.

ForResultAsync<T1, T2, TResult>(long, long, Func<long, T1, T2, CancellationToken, Task<TResult>>, T1, T2, Action<AsyncWorkloadOptions>)

Executes a parallel for loop where the return value of the function delegate worker is stored in the same sequential order as the for loop.

ForResultAsync<T1, T2, T3, TResult>(int, int, Func<int, T1, T2, T3, CancellationToken, Task<TResult>>, T1, T2, T3, Action<AsyncWorkloadOptions>)

Executes a parallel for loop where the return value of the function delegate worker is stored in the same sequential order as the for loop.

ForResultAsync<T1, T2, T3, TResult>(long, long, Func<long, T1, T2, T3, CancellationToken, Task<TResult>>, T1, T2, T3, Action<AsyncWorkloadOptions>)

Executes a parallel for loop where the return value of the function delegate worker is stored in the same sequential order as the for loop.

ForResultAsync<T1, T2, T3, T4, TResult>(int, int, Func<int, T1, T2, T3, T4, CancellationToken, Task<TResult>>, T1, T2, T3, T4, Action<AsyncWorkloadOptions>)

Executes a parallel for loop where the return value of the function delegate worker is stored in the same sequential order as the for loop.

ForResultAsync<T1, T2, T3, T4, TResult>(long, long, Func<long, T1, T2, T3, T4, CancellationToken, Task<TResult>>, T1, T2, T3, T4, Action<AsyncWorkloadOptions>)

Executes a parallel for loop where the return value of the function delegate worker is stored in the same sequential order as the for loop.

ForResultAsync<T1, T2, T3, T4, T5, TResult>(int, int, Func<int, T1, T2, T3, T4, T5, CancellationToken, Task<TResult>>, T1, T2, T3, T4, T5, Action<AsyncWorkloadOptions>)

Executes a parallel for loop where the return value of the function delegate worker is stored in the same sequential order as the for loop.

ForResultAsync<T1, T2, T3, T4, T5, TResult>(long, long, Func<long, T1, T2, T3, T4, T5, CancellationToken, Task<TResult>>, T1, T2, T3, T4, T5, Action<AsyncWorkloadOptions>)

Executes a parallel for loop where the return value of the function delegate worker is stored in the same sequential order as the for loop.

ForResult<TResult>(int, int, Func<int, TResult>, Action<AsyncTaskFactoryOptions>)

Executes a parallel for loop where the return value of the function delegate worker is stored in the same sequential order as the for loop.

ForResult<TResult>(long, long, Func<long, TResult>, Action<AsyncTaskFactoryOptions>)

Executes a parallel for loop where the return value of the function delegate worker is stored in the same sequential order as the for loop.

ForResult<T, TResult>(int, int, Func<int, T, TResult>, T, Action<AsyncTaskFactoryOptions>)

Executes a parallel for loop where the return value of the function delegate worker is stored in the same sequential order as the for loop.

ForResult<T, TResult>(long, long, Func<long, T, TResult>, T, Action<AsyncTaskFactoryOptions>)

Executes a parallel for loop where the return value of the function delegate worker is stored in the same sequential order as the for loop.

ForResult<T1, T2, TResult>(int, int, Func<int, T1, T2, TResult>, T1, T2, Action<AsyncTaskFactoryOptions>)

Executes a parallel for loop where the return value of the function delegate worker is stored in the same sequential order as the for loop.

ForResult<T1, T2, TResult>(long, long, Func<long, T1, T2, TResult>, T1, T2, Action<AsyncTaskFactoryOptions>)

Executes a parallel for loop where the return value of the function delegate worker is stored in the same sequential order as the for loop.

ForResult<T1, T2, T3, TResult>(int, int, Func<int, T1, T2, T3, TResult>, T1, T2, T3, Action<AsyncTaskFactoryOptions>)

Executes a parallel for loop where the return value of the function delegate worker is stored in the same sequential order as the for loop.

ForResult<T1, T2, T3, TResult>(long, long, Func<long, T1, T2, T3, TResult>, T1, T2, T3, Action<AsyncTaskFactoryOptions>)

Executes a parallel for loop where the return value of the function delegate worker is stored in the same sequential order as the for loop.

ForResult<T1, T2, T3, T4, TResult>(int, int, Func<int, T1, T2, T3, T4, TResult>, T1, T2, T3, T4, Action<AsyncTaskFactoryOptions>)

Executes a parallel for loop where the return value of the function delegate worker is stored in the same sequential order as the for loop.

ForResult<T1, T2, T3, T4, TResult>(long, long, Func<long, T1, T2, T3, T4, TResult>, T1, T2, T3, T4, Action<AsyncTaskFactoryOptions>)

Executes a parallel for loop where the return value of the function delegate worker is stored in the same sequential order as the for loop.

ForResult<T1, T2, T3, T4, T5, TResult>(int, int, Func<int, T1, T2, T3, T4, T5, TResult>, T1, T2, T3, T4, T5, Action<AsyncTaskFactoryOptions>)

Executes a parallel for loop where the return value of the function delegate worker is stored in the same sequential order as the for loop.

ForResult<T1, T2, T3, T4, T5, TResult>(long, long, Func<long, T1, T2, T3, T4, T5, TResult>, T1, T2, T3, T4, T5, Action<AsyncTaskFactoryOptions>)

Executes a parallel for loop where the return value of the function delegate worker is stored in the same sequential order as the for loop.

For<T>(int, int, Action<int, T>, T, Action<AsyncTaskFactoryOptions>)

Executes a parallel for loop.

For<T>(long, long, Action<long, T>, T, Action<AsyncTaskFactoryOptions>)

Executes a parallel for loop.

For<T1, T2>(int, int, Action<int, T1, T2>, T1, T2, Action<AsyncTaskFactoryOptions>)

Executes a parallel for loop.

For<T1, T2>(long, long, Action<long, T1, T2>, T1, T2, Action<AsyncTaskFactoryOptions>)

Executes a parallel for loop.

For<T1, T2, T3>(int, int, Action<int, T1, T2, T3>, T1, T2, T3, Action<AsyncTaskFactoryOptions>)

Executes a parallel for loop.

For<T1, T2, T3>(long, long, Action<long, T1, T2, T3>, T1, T2, T3, Action<AsyncTaskFactoryOptions>)

Executes a parallel for loop.

For<T1, T2, T3, T4>(int, int, Action<int, T1, T2, T3, T4>, T1, T2, T3, T4, Action<AsyncTaskFactoryOptions>)

Executes a parallel for loop.

For<T1, T2, T3, T4>(long, long, Action<long, T1, T2, T3, T4>, T1, T2, T3, T4, Action<AsyncTaskFactoryOptions>)

Executes a parallel for loop.

For<T1, T2, T3, T4, T5>(int, int, Action<int, T1, T2, T3, T4, T5>, T1, T2, T3, T4, T5, Action<AsyncTaskFactoryOptions>)

Executes a parallel for loop.

For<T1, T2, T3, T4, T5>(long, long, Action<long, T1, T2, T3, T4, T5>, T1, T2, T3, T4, T5, Action<AsyncTaskFactoryOptions>)

Executes a parallel for loop.