using System; using System.Threading; namespace Cysharp.Threading.Tasks { public partial struct UniTask { /// Run action on the threadPool and return to current SynchronizationContext if configureAwait = true. public static async UniTask Run(Action action, bool configureAwait = true) { if (configureAwait) { var current = SynchronizationContext.Current; await UniTask.SwitchToThreadPool(); try { action(); } finally { if (current != null) { await UniTask.SwitchToSynchronizationContext(current); } } } else { await UniTask.SwitchToThreadPool(); action(); } } /// Run action on the threadPool and return to current SynchronizationContext if configureAwait = true. public static async UniTask Run(Action action, object state, bool configureAwait = true) { if (configureAwait) { var current = SynchronizationContext.Current; await UniTask.SwitchToThreadPool(); try { action(state); } finally { if (current != null) { await UniTask.SwitchToSynchronizationContext(current); } } } else { await UniTask.SwitchToThreadPool(); action(state); } } /// Run action on the threadPool and return to current SynchronizationContext if configureAwait = true. public static async UniTask Run(Func func, bool configureAwait = true) { if (configureAwait) { var current = SynchronizationContext.Current; await UniTask.SwitchToThreadPool(); try { return func(); } finally { if (current != null) { await UniTask.SwitchToSynchronizationContext(current); } } } else { await UniTask.SwitchToThreadPool(); return func(); } } /// Run action on the threadPool and return to current SynchronizationContext if configureAwait = true. public static async UniTask Run(Func func, object state, bool configureAwait = true) { if (configureAwait) { var current = SynchronizationContext.Current; await UniTask.SwitchToThreadPool(); try { return func(state); } finally { if (current != null) { await UniTask.SwitchToSynchronizationContext(current); } } } else { await UniTask.SwitchToThreadPool(); return func(state); } } } }