mirror of
https://github.com/Cysharp/UniTask.git
synced 2026-05-25 01:00:16 +00:00
Add UniTask.Run(Func<UniTask>) overload
This commit is contained in:
@@ -50,6 +50,50 @@ namespace Cysharp.Threading.Tasks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary>
|
||||||
|
public static async UniTask Run(Func<UniTask> action, bool configureAwait = true)
|
||||||
|
{
|
||||||
|
await UniTask.SwitchToThreadPool();
|
||||||
|
|
||||||
|
if (configureAwait)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await action();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
await UniTask.Yield();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await action();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary>
|
||||||
|
public static async UniTask Run(Func<object, UniTask> action, object state, bool configureAwait = true)
|
||||||
|
{
|
||||||
|
await UniTask.SwitchToThreadPool();
|
||||||
|
|
||||||
|
if (configureAwait)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await action(state);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
await UniTask.Yield();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await action(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary>
|
/// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary>
|
||||||
public static async UniTask<T> Run<T>(Func<T> func, bool configureAwait = true)
|
public static async UniTask<T> Run<T>(Func<T> func, bool configureAwait = true)
|
||||||
{
|
{
|
||||||
@@ -71,6 +115,27 @@ namespace Cysharp.Threading.Tasks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary>
|
||||||
|
public static async UniTask<T> Run<T>(Func<UniTask<T>> func, bool configureAwait = true)
|
||||||
|
{
|
||||||
|
await UniTask.SwitchToThreadPool();
|
||||||
|
if (configureAwait)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return await func();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
await UniTask.Yield();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return await func();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary>
|
/// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary>
|
||||||
public static async UniTask<T> Run<T>(Func<object, T> func, object state, bool configureAwait = true)
|
public static async UniTask<T> Run<T>(Func<object, T> func, object state, bool configureAwait = true)
|
||||||
{
|
{
|
||||||
@@ -92,6 +157,28 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return func(state);
|
return func(state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary>
|
||||||
|
public static async UniTask<T> Run<T>(Func<object, UniTask<T>> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user