I'm trying to implement a TaskScheduler that runs all tasks in the order they are submitted, on a single dedicated thread. Here's my implementation, using a BlockingCollection:class SequentialScheduler : TaskScheduler, IDisposable {    readonly BlockingCollection<Task> m_taskQueue = new BlockingCollection<Task>();    readonly Thread m_thread;    bool m_disposed;    public SequentialScheduler() {        m_thread = new Thread(Run);        m_thread.Start();    }    public void Dispose() {        m_disposed = true;    }    void Run() {        while (!m_disposed) {            var task = m_taskQueue.Take();            Debug.Assert(TryExecuteTask(task));        }    }    protected override IEnumerable<Task> GetScheduledTasks() {        return m_taskQueue;    }    protected override void QueueTask(Task task) {        m_taskQueue.Add(task);    }    protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued) {        if (Thread.CurrentThread == m_thread) {            return TryExecuteTask(task);        }        return false;    }}I've played with it a bit and it seems to work well. I have some lingering questions however:As you can tell by the Debug.Assert, I'm not sure how TryExecuteTask could return false and I'm just assuming that given my current implementation, it won't. Can that actually happen and what I should I do if it does?I'm not sure my implementation of TryExecuteTaskInline makes sense as it doesn't get called in my tests. If I understand correctly, this method should run the task synchronously if possible; hence why I'm checking if the current thread is the scheduler's dedicated thread.Also, my implementation of Dispose doesn't wait for the current task to complete, and will cause the rest of the tasks in the queue to just wait there forever, but short of making it blocking and wait for the queue to empty, I don't see how to do that any differently. I just need a way to release the thread.
TaskScheduler that uses a dedicated thread
c#;task parallel library;concurrency