示例#1
0
        /// <summary>
        /// Marks this operation as completed and returns the callback
        /// </summary>
        public Action <Exception, Response> SetCompleted()
        {
            var previousState = Interlocked.CompareExchange(ref _state, StateCompleted, StateInit);

            if (previousState == StateCancelled)
            {
                return(Noop);
            }
            Action <Exception, Response> callback;

            if (previousState == StateInit)
            {
                callback = Interlocked.Exchange(ref _callback, Noop);
                if (Timeout != null)
                {
                    //Cancel it if it hasn't expired
                    Timeout.Cancel();
                }
                return(callback);
            }
            //Operation has timed out
            while (!_timeoutCallbackSet)
            {
                //Wait for the timeout callback to be set
                Thread.SpinWait(10);
            }
            callback = Interlocked.Exchange(ref _callback, Noop);
            return(callback);
        }
示例#2
0
 public static void Resume(Activity act)
 {
     if (App.Kp2a.GetDb().Loaded)
     {
         Timeout.Cancel(act);
     }
 }
示例#3
0
 public static void Resume(Activity act)
 {
     if (App.Kp2a.CurrentDb != null)
     {
         Timeout.Cancel(act);
     }
 }
示例#4
0
        public override void EntityRemoved(Player e)
        {
            base.EntityRemoved(e);

            if (e.Lives == 0)
            {
                if (Timeout != null)
                {
                    Timeout.Cancel();
                }

                Timeout = World.Tweener.Timer(0.2f)
                          .OnComplete(CheckWinner);

                ActivePlayers.Remove(e);
            }

            if (ActivePlayers.Count > 0)
            {
                var remaining = ActivePlayers.Count(p => p.IsAlive);
                if (remaining <= 1)
                {
                    World.Tweener.Timer(1).OnComplete(CheckAdvance);
                }
            }
        }
示例#5
0
 /// <summary>
 /// Removes the context associated with this request, if possible
 /// </summary>
 public void Cancel()
 {
     if (Interlocked.CompareExchange(ref _state, StateCancelled, StateInit) != StateInit)
     {
         return;
     }
     //Remove the closure
     Interlocked.Exchange(ref _callback, Noop);
     if (Timeout != null)
     {
         //Cancel it if it hasn't expired
         //We should not worry about yielding OperationTimedOutExceptions when this is cancelled.
         Timeout.Cancel();
     }
 }
        public Task <Position> GetPositionAsync(int timeout, CancellationToken token, bool includeHeading)
        {
            if (timeout < 0)
            {
                throw new ArgumentOutOfRangeException("timeout");
            }

            IAsyncOperation <Geoposition> pos = GetGeolocator().GetGeopositionAsync(TimeSpan.FromTicks(0), TimeSpan.FromDays(365));

            token.Register(o => ((IAsyncOperation <Geoposition>)o).Cancel(), pos);

            Timeout timer = new Timeout(timeout, pos.Cancel);

            var tcs = new TaskCompletionSource <Position>();

            pos.Completed = (op, s) =>
            {
                timer.Cancel();

                switch (s)
                {
                case AsyncStatus.Canceled:
                    tcs.SetCanceled();
                    break;

                case AsyncStatus.Completed:
                    tcs.SetResult(GetPosition(op.GetResults()));
                    break;

                case AsyncStatus.Error:
                    Exception ex = op.ErrorCode;
                    if (ex is UnauthorizedAccessException)
                    {
                        ex = new GeolocationException(GeolocationError.Unauthorized, ex);
                    }

                    tcs.SetException(ex);
                    break;
                }
            };

            return(tcs.Task);
        }
        public Task <Position> GetPositionAsync(int timeout, bool includeHeading)
        {
            if (timeout < 0)
            {
                throw new ArgumentOutOfRangeException("timeout");
            }

            // The built in timeout does not cancel, it throws an exception, so we'll setup our own.
            IAsyncOperation <Geoposition> pos = GetGeolocator().GetGeopositionAsync(TimeSpan.Zero, TimeSpan.FromDays(365));
            Timeout timer = new Timeout(timeout, pos.Cancel);

            var tcs = new TaskCompletionSource <Position>();

            pos.Completed = (op, s) =>
            {
                timer.Cancel();

                switch (s)
                {
                case AsyncStatus.Canceled:
                    tcs.SetCanceled();
                    break;

                case AsyncStatus.Completed:
                    tcs.SetResult(GetPosition(op.GetResults()));
                    break;

                case AsyncStatus.Error:
                    Exception ex = op.ErrorCode;
                    if (ex is UnauthorizedAccessException)
                    {
                        ex = new GeolocationException(GeolocationError.Unauthorized, ex);
                    }

                    tcs.SetException(ex);
                    break;
                }
            };

            return(tcs.Task);
        }
示例#8
0
 public void Complete(EventPredicate <TEvent, TData> predicate = null)
 {
     _eventActivitySet.UseForCompletion(predicate);
     Then((whenEvent, state) => Timeout.Cancel(state.InstanceId.ToString()), predicate);
 }