private void ReturnToPool() { _token = 0; _tasks = null; _continuation = null; _firstCompletedTaskIndex = 0; _status = ValueSourceStatus.Pending; _exception = null; _continueWith = null; _runWithContinue = false; _pool.Push(this); }
private async LitTask RunTask(LitTask task, int taskIndex, short token) { Exception exception = null; try{ await task; }catch (Exception e) { exception = e; }finally{ if (_token == token && _status == ValueSourceStatus.Pending) { if (exception == null) { _status = ValueSourceStatus.Succeed; } else if (exception is LitCancelException) { _status = ValueSourceStatus.Canceled; } else { _status = ValueSourceStatus.Faulted; _exception = exception; } _firstCompletedTaskIndex = taskIndex; if (_continuation != null) { _continuation(); } else { if (_runWithContinue) { try{ TryInvokeContinueWithAction(); }finally{ this.ReturnToPool(); } } else { //run in sync mode. use will call continute to get the result } } } } }
public void SetException(Exception exception) { if (exception is LitCancelException litCancelException) { _status = ValueSourceStatus.Canceled; } else { _status = ValueSourceStatus.Faulted; } _exception = exception; if (_continuation != null) { _continuation(); } else { if (_runWithContinue) { try{ if (_continueWithAction != null) { try{ _continueWithAction(new LitTaskResult(_exception)); }catch (Exception e) { Utilities.UnityLoopsHelper.ThrowAsync(e); } } else { //continue without handle exception. so throw exception async if (_status == ValueSourceStatus.Faulted) { Utilities.UnityLoopsHelper.ThrowAsync(exception); } } }finally{ ReturnToPool(); } } else { //run in sync mode. user will call continue to finish to task } } }
private void ReturnToPool() { if (_token == 0) { throw new ObjectDisposedException(this.GetType().Name); } _token = 0; if (_stateMachineBox != null) { _stateMachineBox.Return(); _stateMachineBox = null; } _status = ValueSourceStatus.Pending; _continuation = null; _continueWithAction = null; _runWithContinue = false; _pool.Push(this); Diagnostics.Trace.TraceReturn(this); }
public void SetResult() { _status = ValueSourceStatus.Succeed; if (_continuation != null) { // get result will be called in continuation _continuation(); } else { if (_runWithContinue) { //run in async mode, but use continue instead of await try{ if (_continueWithAction != null) { try{ _continueWithAction(new LitTaskResult(null)); }catch (System.Exception e) { Utilities.UnityLoopsHelper.ThrowAsync(e); } } else { //missing continueWithAction } }finally{ ReturnToPool(); } } else { //run in sync mode. user will call continue to finish to task } } }
public WhenAnyResult(int firstCompletedTaskIndex, ValueSourceStatus status, Exception exception) { _firstCompletedTaskIndex = firstCompletedTaskIndex; _status = status; _exception = exception; }