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 resultsDispatch.Parallel(): runs multiple actions in parallel, no return valueDispatch.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
Parallelresult order is not guaranteed —ConcurrentBagis unorderedParallelnull return values are silently skipped — default(T) is added on exceptionFireAndForgetactions run until completion or app shutdown — no cancellation supportFireAndForgetwithoutonErrorcallback 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!!