Skip to main content

MainThreadJobHandler

A class in Root.Township, the global namespace.

Singleton that runs scheduled work on the main thread a bit each frame, within a time budget. Other threads await the returned Entry; world generation uses it for raycasts.

Constructors​

MemberWhat it is
MainThreadJobHandler()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

7 private and internal fields
MemberWhat it is
private MainThreadJobHandler.InternalEntry currentThe job being run, kept across frames when it runs out of time.
private ConcurrentQueue<MainThreadJobHandler.InternalEntry> entriesThread-safe queue of waiting jobs.
private float millisecondAllowanceMilliseconds of jobs allowed per frame (default 3).
private uint nextIndexCounter for job indexes.
private ConcurrentBag<MainThreadJobHandler.InternalEntry> poolPool of reusable entries. Nothing returns entries to it, so it stays empty.
private int remainingCountRead-only inspector count that nothing updates.
private Stopwatch timerStopwatch for the per-frame budget.

Methods​

MemberWhat it is
MainThreadJobHandler.Entry Schedule(Action getTask, CancellationToken cancellationToken = null)Queues an action or a step-by-step IEnumerable job and returns an awaitable entry that gives false if cancelled.
MainThreadJobHandler.Entry Schedule(Func<IEnumerable> getTask, CancellationToken cancellationToken = null)Queues an action or a step-by-step IEnumerable job and returns an awaitable entry that gives false if cancelled.
2 private and internal methods
MemberWhat it is
private void Update()Runs queued jobs in order until millisecondAllowance is used up, resuming the current job next frame.
private IEnumerable Wrap(Action task) (iterator)Turns a plain action into a one-step job.