w00
w00

Reputation: 26772

C# execute code on other thread

I have some trouble with threading in my application. I have a multi-threaded client/server application. I'm also using C# MonoDevelop for Unity3d. Not sure if it makes any difference for the answer. I'll try to explain where my problem is:

Unity works on a single thread. So if i want to instantiate an object which uses the abstract class ScriptableObject from unity, then this must be done on the main thread on which Unity runs.

But my server socket spawns a thread for every connected client, so that incoming data can be processed async. The received data is processed in the OnDataReceived() method (which runs on its own thread)

The problem here is, is that i can't create an instance of a Player object inside the OnDataReceived() thread. Because my Player object inherits from ScriptableObject. Which means this object should be created on the main Unity thread.

But i have no idea how to do that... Is there a way to switch back to the main thread, so i can still create a Player object in the OnDataReceived() method?

Upvotes: 2

Views: 5489

Answers (2)

bobbymcr
bobbymcr

Reputation: 24167

.NET already has a concept of a SynchronizationContext, most often used for UI apps where thread affinity is required to invoke operations on UI controls (e.g. in WPF or WinForms). However, even outside a UI app, you can reuse these concepts for a general purpose thread-affinitized work queue.

This sample shows how to use the WPF DispatcherSynchronizationContext (from WindowsBase.dll) in a simple console application, together with the .NET 4.0 task classes (TaskScheduler / Task) to invoke actions originating on child threads back on the main program thread.

using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Threading;

internal sealed class Program
{
    private static void Main(string[] args)
    {
        int threadCount = 2;
        using (ThreadData data = new ThreadData(threadCount))
        {
            Thread[] threads = new Thread[threadCount];
            for (int i = 0; i < threadCount; ++i)
            {
                threads[i] = new Thread(DoOperations);
            }

            foreach (Thread thread in threads)
            {
                thread.Start(data);
            }

            Console.WriteLine("Starting...");

            // Start and wait here while all work is dispatched.
            data.RunDispatcher();
        }

        // Dispatcher has exited.
        Console.WriteLine("Shutdown.");
    }

    private static void DoOperations(object objData)
    {
        ThreadData data = (ThreadData)objData;
        try
        {
            // Start scheduling operations from child thread.
            for (int i = 0; i < 5; ++i)
            {
                int t = Thread.CurrentThread.ManagedThreadId;
                int n = i;
                data.ExecuteTask(() => SayHello(t, n));
            }
        }
        finally
        {
            // Child thread is done.
            data.OnThreadCompleted();
        }
    }

    private static void SayHello(int requestingThreadId, int operationNumber)
    {
        Console.WriteLine(
            "Saying hello from thread {0} ({1}) on thread {2}.",
            requestingThreadId,
            operationNumber,
            Thread.CurrentThread.ManagedThreadId);
    }

    private sealed class ThreadData : IDisposable
    {
        private readonly Dispatcher dispatcher;
        private readonly TaskScheduler scheduler;
        private readonly TaskFactory factory;
        private readonly CountdownEvent countdownEvent;

        // In this example, we initialize the countdown event with the total number
        // of child threads so that we know when all threads are finished scheduling
        // work.
        public ThreadData(int threadCount)
        {
            this.dispatcher = Dispatcher.CurrentDispatcher;
            SynchronizationContext context = 
                new DispatcherSynchronizationContext(this.dispatcher);
            SynchronizationContext.SetSynchronizationContext(context);
            this.scheduler = TaskScheduler.FromCurrentSynchronizationContext();
            this.factory = new TaskFactory(this.scheduler);
            this.countdownEvent = new CountdownEvent(threadCount);
        }

        // This method should be called by a child thread when it wants to invoke
        // an operation back on the main dispatcher thread.  This will block until
        // the method is done executing.
        public void ExecuteTask(Action action)
        {
            Task task = this.factory.StartNew(action);
            task.Wait();
        }

        // This method should be called by threads when they are done
        // scheduling work.
        public void OnThreadCompleted()
        {
            bool allThreadsFinished = this.countdownEvent.Signal();
            if (allThreadsFinished)
            {
                this.dispatcher.InvokeShutdown();
            }
        }

        // This method should be called by the main thread so that it will begin
        // processing the work scheduled by child threads. It will return when
        // the dispatcher is shutdown.
        public void RunDispatcher()
        {
            Dispatcher.Run();
        }

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        // Dispose all IDisposable resources.
        private void Dispose(bool disposing)
        {
            if (disposing)
            {
                this.countdownEvent.Dispose();
            }
        }
    }
}

Sample output:

Starting...
Saying hello from thread 3 (0) on thread 1.
Saying hello from thread 4 (0) on thread 1.
Saying hello from thread 3 (1) on thread 1.
Saying hello from thread 4 (1) on thread 1.
Saying hello from thread 3 (2) on thread 1.
Saying hello from thread 4 (2) on thread 1.
Saying hello from thread 3 (3) on thread 1.
Saying hello from thread 4 (3) on thread 1.
Saying hello from thread 3 (4) on thread 1.
Saying hello from thread 4 (4) on thread 1.
Shutdown.

Upvotes: 4

GGulati
GGulati

Reputation: 1035

You could communicate with the original thread through a class such as

class Communicator
{
    public static volatile bool CreatePlayer;
}

And in socket code, change the CreatePlayer variable. In the reciever code, check the variable and create a player. After that, set CreatePlayer to false. Similarly with other things. Be careful about manipulating one variable across two threads at the same time - for example, it may be better to have four booleans for CreatePlayer than to have an int NumPlayersToCreate so that both threads aren't trying to constantly access the same data. Of course, you'd have to profile and see. One final thing: make sure the variables changed across both threads are marked as volatile. This makes each thread access the data from main memory rather than keeping it in cache (otherwise, each thread wouldn't notice the data being changed in the other thread's cache).

Yes, this is not the most performant or elegant solution, but it is the simplest. I'm sure someone will suggest a something more involved; if you want, I can do that as well. However, you seem unfamiliar with multithreading, so I thought you'd want something straightforward to get started.

Upvotes: 2

Related Questions