public Task <Position> GetPositionAsync(int timeout = Timeout.Infite, CancellationToken?token = null, bool includeHeading = false) { if (timeout < 0) { throw new ArgumentOutOfRangeException("timeout"); } if (!token.HasValue) { token = CancellationToken.None; } IAsyncOperation <Geoposition> pos = GetGeolocator().GetGeopositionAsync(TimeSpan.FromTicks(0), TimeSpan.FromDays(365)); token.Value.Register(o => ((IAsyncOperation <Geoposition>)o).Cancel(), pos); var 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); }
/// <inheritdoc/> public Task<Position> GetPositionAsync(int timeoutMilliseconds = Timeout.Infite, CancellationToken? token = null, bool includeHeading = false) { if (timeoutMilliseconds < 0 && timeoutMilliseconds != Timeout.Infite) throw new ArgumentOutOfRangeException("timeoutMilliseconds"); if (!token.HasValue) token = CancellationToken.None; IAsyncOperation<Geoposition> pos = GetGeolocator().GetGeopositionAsync(TimeSpan.FromTicks(0), TimeSpan.FromDays(365)); token.Value.Register(o => ((IAsyncOperation<Geoposition>)o).Cancel(), pos); var timer = new Timeout(timeoutMilliseconds, 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; }