WorkRequest

Internal readonly struct representing a unit of work queued for execution by a WorkRunner. Wraps a continuation callback and an optional ManualResetEvent for synchronous Send() operations. Exception-safe — catches and logs exceptions without crashing the runner queue.

Definition

Namespace: Paragon.Core.Async Assembly: Paragon.dll

internal readonly struct WorkRequest

Visibility: internal — not directly accessible by game code.

Remarks

WorkRequest is the bridge between the SynchronizationContext.Post() / Send() calls and the actual callback execution. Two construction patterns exist:

  1. Action-based — Used by the phase-specific Post(Action, WorkExecutionTime) overload. The Action is stored directly as the continuation.

  2. SendOrPostCallback-based — Used by the standard Post(SendOrPostCallback, object) and Send(SendOrPostCallback, object) overrides. The callback and state are captured into a closure: () => delegateCallback.Invoke(state).

Error Handling

Invoke() wraps execution in a try/catch/finally block:

  • try — Calls continuation.Invoke()

  • catch — Catches any Exception and logs it via Debug.LogException() — the runner continues processing subsequent work requests

  • finally — Signals the ManualResetEvent (if present), unblocking any thread waiting on a synchronous Send()

This design ensures that a single failing continuation does not prevent the remaining queued work from executing.

Constructors

WorkRequest(Action, ManualResetEvent)

Creates a work request from a direct Action callback.

Parameter
Type
Default
Description

continuation

Action

(required)

Callback to execute

waitHandle

ManualResetEvent

null

Optional handle to signal on completion (used by Send())

WorkRequest(SendOrPostCallback, object, ManualResetEvent)

Creates a work request from a SendOrPostCallback and state. Captures both into a closure.

Parameter
Type
Default
Description

delegateCallback

SendOrPostCallback

(required)

Standard SynchronizationContext callback

state

object

(required)

State object passed to the callback

waitHandle

ManualResetEvent

null

Optional handle to signal on completion

Methods

Invoke

Executes the continuation, catches and logs any exceptions, and signals the wait handle (if present).

Behavior:

  1. Calls continuation.Invoke()

  2. If an exception occurs, logs it via Debug.LogException(exception) — does not rethrow

  3. In finally, calls waitHandle?.Set() to unblock any waiting thread

Common Pitfalls

circle-exclamation
circle-exclamation

See Also

Last updated