From 81f9c55c7f4b8e81604108781ff8e031df870d63 Mon Sep 17 00:00:00 2001 From: neuecc Date: Thu, 18 Jun 2020 03:27:26 +0900 Subject: [PATCH] Add UniTask.Run(Func) overload --- .../Plugins/UniTask/Runtime/UniTask.Run.cs | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.Run.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.Run.cs index 379b6c2..a7e0bce 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.Run.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.Run.cs @@ -50,6 +50,50 @@ namespace Cysharp.Threading.Tasks } } + /// Run action on the threadPool and return to main thread if configureAwait = true. + public static async UniTask Run(Func action, bool configureAwait = true) + { + await UniTask.SwitchToThreadPool(); + + if (configureAwait) + { + try + { + await action(); + } + finally + { + await UniTask.Yield(); + } + } + else + { + await action(); + } + } + + /// Run action on the threadPool and return to main thread if configureAwait = true. + public static async UniTask Run(Func action, object state, bool configureAwait = true) + { + await UniTask.SwitchToThreadPool(); + + if (configureAwait) + { + try + { + await action(state); + } + finally + { + await UniTask.Yield(); + } + } + else + { + await action(state); + } + } + /// Run action on the threadPool and return to main thread if configureAwait = true. public static async UniTask Run(Func func, bool configureAwait = true) { @@ -71,6 +115,27 @@ namespace Cysharp.Threading.Tasks } } + /// Run action on the threadPool and return to main thread if configureAwait = true. + public static async UniTask Run(Func> func, bool configureAwait = true) + { + await UniTask.SwitchToThreadPool(); + if (configureAwait) + { + try + { + return await func(); + } + finally + { + await UniTask.Yield(); + } + } + else + { + return await func(); + } + } + /// Run action on the threadPool and return to main thread if configureAwait = true. public static async UniTask Run(Func func, object state, bool configureAwait = true) { @@ -92,6 +157,28 @@ namespace Cysharp.Threading.Tasks return func(state); } } + + /// Run action on the threadPool and return to main thread if configureAwait = true. + public static async UniTask Run(Func> func, object state, bool configureAwait = true) + { + await UniTask.SwitchToThreadPool(); + + if (configureAwait) + { + try + { + return await func(state); + } + finally + { + await UniTask.Yield(); + } + } + else + { + return await func(state); + } + } } }