GetFromMainThread

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

Blocking hand-offC# API
void GetFromMainThread(Action function);

Description

Use this method when worker code must synchronously execute a Unity main-thread operation before it can continue. The supplied Action runs on Unity’s main thread, while the calling worker remains blocked until the action is complete.

The method itself does not return a value. When the worker needs a Unity value, assign it inside the action to a dedicated class field such as Vector3 m_position. After GetFromMainThread returns, the worker can continue using that field.

Blocking operationThe current worker cannot continue until Unity’s main thread has completely executed the supplied action.
Store values in a fieldBecause the action has no return value, copy required Unity data into a dedicated class field before continuing on the worker.
Performance: Prefer values cached during Update whenever possible. Repeated blocking calls—especially inside loops—can remove the benefit of background execution.

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 to use it afterward.

Returns

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

Read a Unity value into a class field

using ThreadHarmonizerEngine;
using UnityEngine;

public class DistanceCalculator : MonoBehaviourMultithreaded
{
    private Vector3 m_position;
    private float m_distanceFromOrigin;

    private void Start()
    {
        AddFunction(ParallelUpdate);
    }

    private void ParallelUpdate()
    {
        GetFromMainThread(() =>
        {
            m_position = transform.position;
        });

        // The worker continues only after m_position has been assigned.
        m_distanceFromOrigin = m_position.magnitude;
    }
}