EnqueueOnMainThread

Queues a parameterless operation for asynchronous execution on Unity’s main thread.

Instance hand-offC# API
void EnqueueOnMainThread(Action function);

Description

Use this method from worker code when the final operation must access a Unity object or another main-thread-only API. The worker queues the operation and continues without waiting for it to finish.

Asynchronous hand-offCode after EnqueueOnMainThread must not assume that the queued operation has already run.
Unity-side applyKeep heavy calculation on the worker and make the queued main-thread operation as small as practical.

Parameters

NameTypeDescription
functionActionA parameterless function with no return value. This function is executed later on Unity’s main thread.

Calculate on a worker and apply on the main thread

using ThreadHarmonizerEngine;
using UnityEngine;

public class PositionCalculator : MonoBehaviourMultithreaded
{
    private Vector3 startPosition;

    private void Start()
    {
        startPosition = transform.position;
        AddFunction(ParallelUpdate);
    }

    private void ParallelUpdate()
    {
        Vector3 nextPosition = startPosition + Vector3.right * 2f;

        EnqueueOnMainThread(() =>
        {
            transform.position = nextPosition;
        });
    }
}