Yeah69 (4) [Avatar] Offline
#1
I really like the idea of the ConcurrencyProvider (Appendix C) for injecting of a composition of schedulers over IoC:

class ConcurrencyProvider : IConcurrencyProvider
{
    public ConcurrencyProvider()
    {
        TimeBasedOperations = DefaultScheduler.Instance;
        Task = TaskPoolScheduler.Default;
        Thread = NewThreadScheduler.Default;
        Dispatcher = DispatcherScheduler.Current;
    }

    public IScheduler TimeBasedOperations { get; }
    public IScheduler Task { get; }
    public IScheduler Thread { get; }
    public IScheduler Dispatcher { get;  }
}


Hence, I implemented it for a project at my company. However, I ran into an error, because the constructor of the ConcurrencyProvider was called from a worker thread the DispatcherScheduler didn't represent the UI thread. A small improvement fixed this issue:

class ConcurrencyProvider
{
    public ConcurrencyProvider()
    {
        TimeBasedOperations = DefaultScheduler.Instance;
        Task = TaskPoolScheduler.Default;
        Thread = NewThreadScheduler.Default;
        Dispatcher = new DispatcherScheduler(Application.Current.Dispatcher);
    }

    public IScheduler TimeBasedOperations { get; }
    public IScheduler Task { get; }
    public IScheduler Thread { get; }
    public IScheduler Dispatcher { get; }
}


This makes sure that independent from the calling thread the intended DispatcherScheduler is chosen in the constructor.