/// <summary> /// Runs the specified work on a new thread and returns a UnityTask object that represents the work /// </summary> /// <typeparam name="TResult">The return type of the UnityTask</typeparam> /// <param name="action">The work to execute asynchronously</param> /// <returns>A UnityTask object that represents the work to be run on a new thread</returns> public static UnityTask <TResult> Run <TResult>(Func <TResult> action) { UnityTask <TResult> unityTask = new UnityTask <TResult>(action); unityTask.Start(); return(unityTask); }
/// <summary> /// Runs the specified work on a new thread and returns a UnityTask object that represents the work /// </summary> /// <param name="action">The work to execute asynchronously</param> /// <returns>A UnityTask object that represents the work to be run on a new thread</returns> public static UnityTask Run(Action action) { UnityTask unityTask = new UnityTask(action); unityTask.Start(); return(unityTask); }
/// <summary> /// Creates a continuation that executes asynchronously when the target UnityTask completes /// </summary> /// <typeparam name="TResult">The type of the result produced by the continuation</typeparam> /// <param name="function">A function to run when the UnityTask completes. When run, the delegate will be passed the completed UnityTask as an argument</param> /// <returns>A new continuation UnityTask</returns> public UnityTask <TResult> ContinueWith <TResult>(Func <UnityTask, TResult> function) { Func <TResult> wrapperFunc = () => { return(function(this)); }; UnityTask <TResult> continuation = new UnityTask <TResult>(wrapperFunc); _continuation = continuation; return(continuation); }
/// <summary> /// Creates a continuation that executes asynchronously when the target UnityTask completes /// </summary> /// <param name="action">An action to run when the UnityTask completes. When run, the delegate will be passed the completed UnityTask as an argument</param> /// <returns>A new continuation UnityTask</returns> public UnityTask ContinueWith(Action <UnityTask> action) { Action wrapper = () => { try { action(this); } catch { State = UnityTaskState.Faulted; throw; } }; UnityTask continuation = new UnityTask(wrapper); _continuation = continuation; return(continuation); }