Home > Back-end >  Custom cross-platform thread dispatcher for console / asp.net application
Custom cross-platform thread dispatcher for console / asp.net application

Time:02-21

I’ve got a .NET application that needs to call a COM object (it always has to be called from the same thread). As I have multiple threads in the application, I need to invoke an action on another thread.
The application does not have a (standard) message loop and I don’t really like the idea to add WPF / WinForms just to have a Dispatcher.
What would be a safe and effective way to implement a custom "message loop" / queue that allows invoking an Action / Func (with return type) on another thread?
It would also be nice to have a cross-platform solution for this problem.

CodePudding user response:

Based on the information of @theodor-zoulias, I came up with this solution.
Disclaimer: Might be that this is actually a very bad design!

public sealed class DispatcherLoop : IDisposable
{
    #region Instance
    private DispatcherLoop() { }

    static Dictionary<int, DispatcherLoop> dispatcherLoops = new();
    public static DispatcherLoop Current
    {
        get
        {
            int threadId = Thread.CurrentThread.ManagedThreadId;
            if (dispatcherLoops.ContainsKey(threadId))
                return dispatcherLoops[threadId];

            DispatcherLoop dispatcherLoop = new()
            {
                ThreadId = Thread.CurrentThread.ManagedThreadId
            };
            dispatcherLoops.Add(threadId, dispatcherLoop);
            return dispatcherLoop;
        }
    }
    #endregion

    bool isDisposed = false;
    public void Dispose()
    {
        if (isDisposed)
            throw new ObjectDisposedException(null);

        _queue.CompleteAdding();
        _queue.Dispose();
        dispatcherLoops.Remove(ThreadId);
        isDisposed = true;
    }

    public int ThreadId { get; private set; } = -1;
    public bool IsRunning { get; private set; } = false;

    BlockingCollection<Task> _queue = new();
    public void Run()
    {
        if (isDisposed)
            throw new ObjectDisposedException(null);

        if (ThreadId != Thread.CurrentThread.ManagedThreadId)
            throw new InvalidOperationException($"The {nameof(DispatcherLoop)} has been created for a different thread!");

        if (IsRunning)
            throw new InvalidOperationException("Already running!");

        IsRunning = true;

        try
        {
            // ToDo: `RunSynchronously` is not guaranteed to be executed on this thread (see comments below)!
            foreach (var task in _queue.GetConsumingEnumerable())
                task?.RunSynchronously();
        }
        catch (ObjectDisposedException) { }

        IsRunning = false;
    }

    public void BeginInvoke(Task task)
    {
        if (isDisposed)
            throw new ObjectDisposedException(null);

        if (!IsRunning)
            throw new InvalidOperationException("Not running!");

        if (ThreadId == Thread.CurrentThread.ManagedThreadId)
            task?.RunSynchronously();
        else
            _queue.Add(task);
    }

    public void Invoke(Action action)
    {
        if (isDisposed)
            throw new ObjectDisposedException(null);

        Task task = new(action);
        BeginInvoke(task);
        task.GetAwaiter().GetResult();
    }

    public T Invoke<T>(Func<T> action)
    {
        if (isDisposed)
            throw new ObjectDisposedException(null);

        Task<T> task = new(action);
        BeginInvoke(task);
        return task.GetAwaiter().GetResult();
    }
}

CodePudding user response:

You should use Microsoft's Reactive Framework (aka Rx) - NuGet System.Reactive and add using System.Reactive.Linq; - then you can do this:

var els = new EventLoopScheduler();

Then you can do things like this:

els.Schedule(() => Console.WriteLine("Hello, World!"));

The EventLoopScheduler spins up its own thread and you can ask it to schedule any work on it you like - it'll always be that thread.

When you're finished with the scheduler, just call els.Dispose() to shut down it down cleanly.

There are also plenty of overloads for scheduling code in the future. It's a very powerful class.

  • Related