/// <summary> /// Aborts this UnityTask - will throw a ThreadAbortedException /// </summary> public void Abort() { if (State == UnityTaskState.Running) { _thread.Abort(); State = UnityTaskState.Aborted; } }
/// <summary> /// Starts this UnityTask /// </summary> public void Start() { if (State == UnityTaskState.Created) { State = UnityTaskState.Running; _thread.Start(); } }
/// <summary> /// Initialises a new UnityTask with the specified action /// </summary> /// <param name="action">The delegate that represents the code to execute in the UnityTask</param> public UnityTask(Action action) { Action wrapperAction = () => { try { action(); } catch { State = UnityTaskState.Faulted; } }; Initialise(wrapperAction); }
protected void Initialise(Action action) { action += () => { if (State != UnityTaskState.Aborted && State != UnityTaskState.Faulted) { State = UnityTaskState.Finished; } if (_continuation != null && State != UnityTaskState.Aborted) { _continuation.Start(); } }; _thread = new UnityThread(action); State = UnityTaskState.Created; }
/// <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); }