Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
		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;
		}
Exemplo n.º 4
0
		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;
		}