Table of Contents

Dispatch

Description

Static helpers for running functions in parallel or as fire-and-forget background tasks.

Features

  • Dispatch.Parallel<T>(): runs multiple functions in parallel, collects and returns results
  • Dispatch.Parallel(): runs multiple actions in parallel, no return value
  • Dispatch.FireAndForget(): runs actions in background threads — non-blocking, caller never waits

Behavior

Parallel uses Parallel.ForEach with max degree of parallelism set to ProcessorCount / 4 — intentionally conservative to avoid starving the web server thread pool.

Default timeout is 30 seconds — configurable per call. If timeout is hit, collected results so far are returned and an error is logged. Exceptions inside individual functions are caught, logged and skipped — remaining functions continue.

FireAndForget wraps each action in Task.Run — each action runs independently on the thread pool. Exceptions are caught and passed to the optional onError callback. If no callback is provided exceptions are silently swallowed.

Caveats

  • Parallel result order is not guaranteed — ConcurrentBag is unordered
  • Parallel null return values are silently skipped — default(T) is added on exception
  • FireAndForget actions run until completion or app shutdown — no cancellation support
  • FireAndForget without onError callback swallows all exceptions silently
Tip

Use Parallel for independent IO calls that can run simultaneously — fetching from multiple APIs, loading multiple config files. Use FireAndForget for work that must not block the caller — sending emails, writing audit logs.

!!SKIP!!