Calling function in main thread from separate thread

Updated on December 14, 2015 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on December 13, 2015

Hey guys!
I have a running separate thread and I need to call a function in the main thread once the second one finishes. The problem is that I don’t know how.
And just to make it a lot more fun: This script is running ’bout 6500 times in the scene, so please don’t post Co-Routine solutions.
Thanks 🙂

  • Liked by
Reply
1 on December 14, 2015

So what I like to do is something like this:

List<Action> ActionQueue = new List<Action>();
void Update()
{
    while (ActionQueue.Count > 0)
    {
        ActionQueue[0]();
        ActionQueue.RemoveAt(0);
    }
}

When you want to add something to the queue, then you can do something like this:

ActionQueue.Add( () =>
{
    // Your code goes here
});

If you want to know more, then be sure to check out (in order)

  • Delegates
  • Funcs, Actions and Annonymous/unnamed delegates
  • Lambda expressions
on December 14, 2015

Thanks, I’ll take a look at it.

Show more replies
  • Liked by
Reply
Cancel