GetFromMainThread

Executes a parameterless action on Unity’s main thread and blocks the calling background thread until that action has finished.

Static blocking hand-offC# API
static void GetFromMainThread(Action function);

Description

This static variant synchronously executes a Unity main-thread operation without requiring the calling script to inherit from MonoBehaviourMultithreaded. The supplied Action runs on Unity’s main thread, and the calling background thread remains blocked until the action is complete.

The method returns no value. To transfer a Unity value back to the waiting worker, assign it inside the action to a dedicated class field such as Vector3 m_position. The worker can safely continue with that field after GetFromMainThread has returned.

Synchronous waitThe calling background thread cannot continue until Unity’s main thread has completely executed the action.
Use a dedicated fieldStore required Unity data in a class field rather than expecting the action or method to return a value.
Performance: Use this method sparingly. Cache main-thread data ahead of time when possible, and avoid repeated blocking calls inside loops.

Parameters

NameTypeDescription
functionActionA parameterless action executed on Unity’s main thread. It has no return value. Assign required data to a class field when the waiting worker needs it afterward.

Returns

No return value: The method returns void. Values needed by the background thread must be assigned inside the action to an accessible field.

Read a Unity value from a worker task

using ThreadHarmonizerEngine;
using UnityEngine;

public class StaticDistanceCalculator : MonoBehaviour
{
    private Vector3 m_position;
    private float m_distanceFromOrigin;

    private void Start()
    {
        ThreadHarmonizerManager.EnqueueOnWorker(() =>
        {
            ThreadHarmonizerManager.GetFromMainThread(() =>
            {
                m_position = transform.position;
            });

            // Execution resumes here after the main-thread action is complete.
            m_distanceFromOrigin = m_position.magnitude;
        });
    }
}