Add UniTask.Run(Func<UniTask>) overload

pull/101/head
neuecc 2020-06-18 03:27:26 +09:00
parent 0640f278cc
commit 81f9c55c7f
1 changed files with 87 additions and 0 deletions

View File

@ -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);
}
}
} }
} }