示例#1
0
        public bool Run()
        {
            if (_status != PAsyncTaskStatus.NotStarted)
            {
                Debug.LogErrorFormat("Cannot start {0} cause it is {1}", GetType(), _status);
                return(false);
            }

            if (!_owner.Add(this))
            {
                Debug.LogErrorFormat("Cannot add {0} to manager", GetType());
                return(false);
            }

            try {
                _coroutine = _owner.StartCoroutine(InternalRunCoroutine());
                _status    = PAsyncTaskStatus.Running;
            } catch (Exception e) {
                Debug.LogErrorFormat("Cannot start {0} cause of exception", GetType());
                Debug.LogException(e);

                _status = PAsyncTaskStatus.UnexpectedError;
                _owner.Remove(_id);

                return(false);
            }

            return(true);
        }
示例#2
0
        protected override bool TryDispose()
        {
            if (_status == PAsyncTaskStatus.Disposed)
            {
                return(true);
            }

            InternalCancel();

            InternalDisposeCoroutine();

            if (_task == null || _task.IsCompleted)
            {
                InternalDisposeCTS();
                InternalDisposeTask();
            }

            if (!InternalIsAllDisposed())
            {
                return(false);
            }

            _status = PAsyncTaskStatus.Disposed;
            return(true);
        }
示例#3
0
        private IEnumerator InternalRunCoroutine()
        {
            try {
                _cts  = new CancellationTokenSource();
                _ct   = _cts.Token;
                _task = Task.Run(AsyncTask, _ct);
            } catch (Exception e) {
                Debug.LogErrorFormat("Cannot start {0} cause of exception", GetType());
                Debug.LogException(e);

                _status = PAsyncTaskStatus.Failed;
                _callbackFailure?.Invoke();
                _owner.Remove(_id);

                yield break;
            }

            // guarantee task will not complete instant
            yield return(null);

            while (!_task.IsCompleted)
            {
                yield return(null);
            }

            if (_task.IsFaulted)
            {
                Debug.LogErrorFormat("Task {0} failed cause of exception", GetType());
                Debug.LogException(_task.Exception);

                _status = PAsyncTaskStatus.Failed;
                _callbackFailure?.Invoke();
                _owner.Remove(_id);

                yield break;
            }

            if (_task.IsCanceled)
            {
                Debug.LogFormat("Task {0} cancelled", GetType());

                _status = PAsyncTaskStatus.Cancelled;
                _owner.Remove(_id);

                yield break;
            }

            _status = PAsyncTaskStatus.Success;
            _callbackSuccess?.Invoke(_task.Result);
            _owner.Remove(_id);
        }
示例#4
0
        protected PAsyncTask(Action <T> callbackSuccess, Action callbackFailure)
        {
            _owner           = PInternalAsyncTaskManager.AsOwner();
            _id              = _owner.GenerateID();
            _callbackSuccess = callbackSuccess;
            _callbackFailure = callbackFailure;

            _coroutine = null;

            _task = null;
            _cts  = null;
            _ct   = CancellationToken.None;

            _status = PAsyncTaskStatus.NotStarted;
        }
示例#5
0
        private void InternalCancel()
        {
            if (_cts == null || _status != PAsyncTaskStatus.Running)
            {
                return;
            }

            try {
                _cts.Cancel();
                _status = PAsyncTaskStatus.CancelRequested;
            } catch (Exception e) {
                Debug.LogErrorFormat("Cannot cancel {0} cause of exception", GetType());
                Debug.LogException(e);
                _status = PAsyncTaskStatus.UnexpectedError;
            }
        }