CustomThreadPool

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

// https://stackoverflow.com/questions/75598227/custom-taskscheduler-that-gets-used-across-awaits
public class Program
{
    public static async Task Main()
    {
        using (CustomThreadPool scheduler = new())
        {
            await Task.Factory.StartNew(() => VerifyTaskSchedulerRemainsCustom(),
                CancellationToken.None, TaskCreationOptions.DenyChildAttach,
                scheduler).Unwrap();

            Console.WriteLine($"Out: {TaskScheduler.Current}");
            await Task.Delay(500); // Give some time for the pending async void.
        }
        Console.WriteLine($"Finished");
    }

    private static async Task VerifyTaskSchedulerRemainsCustom()
    {
        Console.WriteLine($"1: {TaskScheduler.Current}");
        await Task.Yield();
        Console.WriteLine($"2: {TaskScheduler.Current}");
        await Task.Delay(100).ConfigureAwait(true);
        Console.WriteLine($"3: {TaskScheduler.Current}");
        MyAsyncVoid();

        async void MyAsyncVoid()
        {
            Console.WriteLine($"4: {TaskScheduler.Current}");
            await Task.Yield();
            Console.WriteLine($"5: {TaskScheduler.Current}");
            await Task.Delay(100).ConfigureAwait(true);
            Console.WriteLine($"6: {TaskScheduler.Current}");
        }
    }
}

public class CustomThreadPool : TaskScheduler, IDisposable
{
    private readonly BlockingCollection<Task> _queue;
    private readonly Thread[] _threads;

    public CustomThreadPool(int threadsCount = 1)
    {
        _queue = new BlockingCollection<Task>();
        _threads = Enumerable.Range(0, threadsCount).Select(_ => new Thread(() =>
        {
            foreach (var task in _queue.GetConsumingEnumerable())
                TryExecuteTask(task);
        }) { IsBackground = true }).ToArray();
        Array.ForEach(_threads, t => t.Start());
    }

    protected override void QueueTask(Task task)
    {
        try { _queue.Add(task); }
        catch (ObjectDisposedException)
        {
            ThreadPool.QueueUserWorkItem(_ => throw new TaskSchedulerException());
        }
    }

    protected override bool TryExecuteTaskInline(Task task,
        bool taskWasPreviouslyQueued)
    {
        if (Array.IndexOf(_threads, Thread.CurrentThread) < 0) return false;
        return TryExecuteTask(task);
    }

    public override int MaximumConcurrencyLevel => _threads.Length;
    protected override IEnumerable<Task> GetScheduledTasks() => _queue;

    public void Dispose()
    {
        _queue.CompleteAdding();
        Array.ForEach(_threads, t => t.Join());
        _queue.Dispose();
    }
}
;