Пример #1
0
 public AsyncOperationResult DispatchSynchronously(object?arg)
 {
     if (Interlocked.Exchange(ref _awaitsResolution, 0) == 1)
     {
         if (_task == null)
         {
             try
             {
                 return(AsyncOperationResult.Success(_action(arg).Result));
             }
             catch (Exception ex)
             {
                 return(AsyncOperationResult.Failure(ex));
             }
         }
         else
         {
             return(_task.Result);
         }
     }
     else
     {
         throw new NotImplementedException();
     }
 }
Пример #2
0
        public void Dispatch(object?arg, Action <AsyncOperationResult> callback)
        {
            var threadStart = new ThreadStart(() =>
            {
                try
                {
                    var result = AsyncOperationResult.Success(_action(arg));
                    if (Interlocked.Exchange(ref _awaitsResolution, 0) == 1)
                    {
                        _result = result;
                        callback(result);
                    }
                }
                catch (Exception ex)
                {
                    if (Interlocked.Exchange(ref _awaitsResolution, 0) == 1)
                    {
                        _result = AsyncOperationResult.Failure(ex);
                        callback(_result);
                    }
                }
            });

            _thread = new Thread(threadStart);
            _thread.Start();
        }
Пример #3
0
 public void Dispatch(object?arg, Action <AsyncOperationResult> callback)
 {
     if (_operation == null)
     {
         _operation = _action(arg);
     }
     _operation.Catch <Exception>(ex => callback(AsyncOperationResult.Failure(ex)));
     _operation.ThenWait(() => callback(AsyncOperationResult.Success()));            //operation.ThenWait(() => callback(null));
 }
Пример #4
0
 internal void Accept(object?result)
 {
     State  = AsyncOperationState.Completed;
     Result = AsyncOperationResult.Success(result);
     while (_continuations.TryDequeue(out var continuation))
     {
         continuation.Dispatch(Result.Value);
     }
 }
Пример #5
0
 internal virtual AsyncOperationResult Handle(Exception exception)
 {
     if (_catchHandlers.TryHandle(exception))
     {
         return(AsyncOperationResult.Success());
     }
     else
     {
         return(AsyncOperationResult.Failure(exception));
     }
 }
Пример #6
0
 public AsyncOperationResult DispatchSynchronously(object?arg)
 {
     try
     {
         return(AsyncOperationResult.Success(_action(arg)));
     }
     catch (Exception ex)
     {
         return(AsyncOperationResult.Failure(ex));
     }
 }
Пример #7
0
 public void Dispatch(object?arg, Action <AsyncOperationResult> callback)
 {
     try
     {
         callback(AsyncOperationResult.Success(_action(arg)));
     }
     catch (Exception ex)
     {
         callback(AsyncOperationResult.Failure(ex));
     }
 }
Пример #8
0
 private void _complete(AsyncOperationResult result)
 {
     if (result.IsSuccessful)
     {
         Accept(result.Value);
     }
     else
     {
         Reject(result.Error !);
     }
 }
Пример #9
0
        internal override AsyncOperationResult Handle(Exception exception)
        {
            TPass resultValue;

            if (_catchHandlers.TryHandle(exception, out resultValue))
            {
                return(AsyncOperationResult.Success(resultValue));
            }
            else
            {
                return(AsyncOperationResult.Failure(exception));
            }
        }
Пример #10
0
        protected AsyncOperationResult _Wait()
        {
            switch (State)
            {
            case AsyncOperationState.Deferred:
                throw new InvalidOperationException("Cannot wait for an AsyncOperation that is Deferred");

            case AsyncOperationState.Aborted:
                return(AsyncOperationResult.Failure(new InvalidOperationException("Cannot wait for an operation that has been aborted.")));

            case AsyncOperationState.Failed:
                return(AsyncOperationResult.Failure(new AsyncException(Result !.Error !)));

            case AsyncOperationState.Initial:
                if (_preceedingOperation == null)
                {
                    return(Operator !.DispatchSynchronously(null));
                }
                var pass = _preceedingOperation._Wait();
                if (!pass.IsSuccessful)
                {
                    pass = Handle(pass.Error !);
                }
                if (pass.IsSuccessful)
                {
                    return(Operator !.DispatchSynchronously(pass.Value));
                }
                else
                {
                    return(pass);
                }

            case AsyncOperationState.Dispatched:
                if (_preceedingOperation != null)
                {
                    return(Operator !.DispatchSynchronously(_preceedingOperation.Result));
                }
                else
                {
                    return(Operator !.DispatchSynchronously(null));
                }

            case AsyncOperationState.Completed:
                return(Result !);

            default:
                throw new NotSupportedException("Unsupported AsyncOperationState '" + Enum.GetName(typeof(AsyncOperationState), State) + "'");
            }
        }
Пример #11
0
 public AsyncOperationResult DispatchSynchronously(object?arg)
 {
     if (_operation == null)
     {
         _operation = _action(arg);
     }
     _operation.Dispatch(arg);
     try
     {
         _operation.Wait();
         return(AsyncOperationResult.Success());
     }
     catch (Exception ex)
     {
         return(AsyncOperationResult.Failure(ex));
     }
 }
Пример #12
0
 public void Dispatch(object?arg, Action <AsyncOperationResult> callback)
 {
     _actionHandle = new AsyncEventArgs(() =>
     {
         try
         {
             _result = AsyncOperationResult.Success(_action(arg));
             callback(_result);
         }
         catch (Exception ex)
         {
             _result = AsyncOperationResult.Failure(ex);
             callback(_result);
         }
     });
     Async.Dispatch(_actionHandle);
 }
Пример #13
0
 public AsyncOperationResult DispatchSynchronously(object?arg)
 {
     if (Interlocked.Exchange(ref _awaitsResolution, 0) == 1)
     {
         try
         {
             return(_result = AsyncOperationResult.Success(_action(arg)));
         }
         catch (Exception ex)
         {
             return(_result = AsyncOperationResult.Failure(ex));
         }
     }
     else
     {
         return(_result !);
     }
 }
Пример #14
0
 public AsyncOperationResult DispatchSynchronously(object?arg)
 {
     if (_actionHandle.Resolve())
     {
         try
         {
             return(_result = AsyncOperationResult.Success(_action(arg)));
         }
         catch (Exception ex)
         {
             return(_result = AsyncOperationResult.Failure(ex));
         }
     }
     else
     {
         return(_result !);
     }
 }
Пример #15
0
        public void Dispatch(object?arg, Action <AsyncOperationResult> callback)
        {
            _task = Task.Run(async() => {
                try
                {
                    return(AsyncOperationResult.Success(await _action(arg)));
                }
                catch (Exception ex)
                {
                    return(AsyncOperationResult.Failure(ex));
                }
            }, _cancellation.Token);

            _task.ContinueWith(task => {
                if (Interlocked.Exchange(ref _awaitsResolution, 0) == 1)
                {
                    callback(task.Result);
                }
            });
        }
Пример #16
0
 internal AsyncOperation(AsyncOperationResult result)
 {
     Result = result;
     State  = result.IsSuccessful ? AsyncOperationState.Completed : AsyncOperationState.Failed;
 }
Пример #17
0
 public AsyncOperation(TPass value)
     : base(AsyncOperationResult.Success(value))
 {
 }
Пример #18
0
 public static IAsyncOperation Fail <TException>(TException ex) where TException : Exception
 {
     return(new AsyncOperation(AsyncOperationResult.Failure(ex)));
 }