diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.Run.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.Run.cs index a7e0bce..40cea40 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.Run.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.Run.cs @@ -1,16 +1,21 @@ #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member using System; +using System.Threading; namespace Cysharp.Threading.Tasks { public partial struct UniTask { /// Run action on the threadPool and return to main thread if configureAwait = true. - public static async UniTask Run(Action action, bool configureAwait = true) + public static async UniTask Run(Action action, bool configureAwait = true, CancellationToken cancellationToken = default) { + cancellationToken.ThrowIfCancellationRequested(); + await UniTask.SwitchToThreadPool(); + cancellationToken.ThrowIfCancellationRequested(); + if (configureAwait) { try @@ -26,13 +31,19 @@ namespace Cysharp.Threading.Tasks { action(); } + + cancellationToken.ThrowIfCancellationRequested(); } /// Run action on the threadPool and return to main thread if configureAwait = true. - public static async UniTask Run(Action action, object state, bool configureAwait = true) + public static async UniTask Run(Action action, object state, bool configureAwait = true, CancellationToken cancellationToken = default) { + cancellationToken.ThrowIfCancellationRequested(); + await UniTask.SwitchToThreadPool(); + cancellationToken.ThrowIfCancellationRequested(); + if (configureAwait) { try @@ -48,13 +59,19 @@ namespace Cysharp.Threading.Tasks { action(state); } + + cancellationToken.ThrowIfCancellationRequested(); } /// Run action on the threadPool and return to main thread if configureAwait = true. - public static async UniTask Run(Func action, bool configureAwait = true) + public static async UniTask Run(Func action, bool configureAwait = true, CancellationToken cancellationToken = default) { + cancellationToken.ThrowIfCancellationRequested(); + await UniTask.SwitchToThreadPool(); + cancellationToken.ThrowIfCancellationRequested(); + if (configureAwait) { try @@ -70,13 +87,19 @@ namespace Cysharp.Threading.Tasks { await action(); } + + cancellationToken.ThrowIfCancellationRequested(); } /// 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) + public static async UniTask Run(Func action, object state, bool configureAwait = true, CancellationToken cancellationToken = default) { + cancellationToken.ThrowIfCancellationRequested(); + await UniTask.SwitchToThreadPool(); + cancellationToken.ThrowIfCancellationRequested(); + if (configureAwait) { try @@ -92,12 +115,19 @@ namespace Cysharp.Threading.Tasks { await action(state); } + + cancellationToken.ThrowIfCancellationRequested(); } /// Run action on the threadPool and return to main thread if configureAwait = true. - public static async UniTask Run(Func func, bool configureAwait = true) + public static async UniTask Run(Func func, bool configureAwait = true, CancellationToken cancellationToken = default) { + cancellationToken.ThrowIfCancellationRequested(); + await UniTask.SwitchToThreadPool(); + + cancellationToken.ThrowIfCancellationRequested(); + if (configureAwait) { try @@ -107,6 +137,7 @@ namespace Cysharp.Threading.Tasks finally { await UniTask.Yield(); + cancellationToken.ThrowIfCancellationRequested(); } } else @@ -116,9 +147,14 @@ 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) + public static async UniTask Run(Func> func, bool configureAwait = true, CancellationToken cancellationToken = default) { + cancellationToken.ThrowIfCancellationRequested(); + await UniTask.SwitchToThreadPool(); + + cancellationToken.ThrowIfCancellationRequested(); + if (configureAwait) { try @@ -127,20 +163,28 @@ namespace Cysharp.Threading.Tasks } finally { + cancellationToken.ThrowIfCancellationRequested(); await UniTask.Yield(); + cancellationToken.ThrowIfCancellationRequested(); } } else { - return await func(); + var result = await func(); + cancellationToken.ThrowIfCancellationRequested(); + return result; } } /// 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) + public static async UniTask Run(Func func, object state, bool configureAwait = true, CancellationToken cancellationToken = default) { + cancellationToken.ThrowIfCancellationRequested(); + await UniTask.SwitchToThreadPool(); + cancellationToken.ThrowIfCancellationRequested(); + if (configureAwait) { try @@ -150,6 +194,7 @@ namespace Cysharp.Threading.Tasks finally { await UniTask.Yield(); + cancellationToken.ThrowIfCancellationRequested(); } } else @@ -159,10 +204,14 @@ namespace Cysharp.Threading.Tasks } /// 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) + public static async UniTask Run(Func> func, object state, bool configureAwait = true, CancellationToken cancellationToken = default) { + cancellationToken.ThrowIfCancellationRequested(); + await UniTask.SwitchToThreadPool(); + cancellationToken.ThrowIfCancellationRequested(); + if (configureAwait) { try @@ -171,12 +220,16 @@ namespace Cysharp.Threading.Tasks } finally { + cancellationToken.ThrowIfCancellationRequested(); await UniTask.Yield(); + cancellationToken.ThrowIfCancellationRequested(); } } else { - return await func(state); + var result = await func(state); + cancellationToken.ThrowIfCancellationRequested(); + return result; } } }