Task <bool> StartService() { //bool result = true; //var NonUItask = Task.Run(() => return(Task.Run(() => { Console.WriteLine("[LocationService.StartService()] start"); try { locator = new Locator(LocationType.Hybrid); locator.ServiceStateChanged += Locator_ServiceStateChanged; locator.Start(); } catch (Exception ee) { Console.WriteLine("\n\n\n\n\n LocationService Exception : " + ee.Message + ", " + ee.StackTrace + ", " + ee.InnerException); if (locator != null) { locator.ServiceStateChanged -= Locator_ServiceStateChanged; locator.Dispose(); locator = null; } return false; } Console.WriteLine("[LocationService.StartService()] end locator: " + locator); return true; })); }
public void LocatorDispose() { if (_locatorInitialized) { _locator.ServiceStateChanged -= LocatorServiceStateChanged; _locator.Stop(); _locator.Dispose(); _locator = null; _locatorInitialized = false; } }
/// <summary> /// Releases all resources used by the current instance. /// </summary> /// <param name="disposing">Indicates whether the method call comes from a Dispose method or from a finalizer.</param> protected virtual void Dispose(bool disposing) { if (_disposed) { return; } if (disposing) { Stop(); _locator.Dispose(); } _disposed = true; }
/// <summary> /// Creates request to a location service. /// </summary> /// <returns>Returns task with lovation service response.</returns> public async Task <LocationServiceResponse> GetLocationAsync() { if (PrivilegeManager.AllPermissionsGranted()) { var taskCompletionSource = new TaskCompletionSource <Location>(); Locator locator = null; try { locator = new Locator(LocationType.Hybrid); void OnLocatorServiceStateChanged(object sender, ServiceStateChangedEventArgs e) { if (e.ServiceState == ServiceState.Enabled) { locator.ServiceStateChanged -= OnLocatorServiceStateChanged; var receivedLocation = locator.GetLocation(); taskCompletionSource.SetResult(receivedLocation); } } locator.ServiceStateChanged += OnLocatorServiceStateChanged; locator.Start(); var location = await taskCompletionSource.Task; _loggerService.Verbose($"{location.Latitude}, {location.Longitude}"); return(new LocationServiceResponse(true, true, new Geocoordinates(location.Latitude, location.Longitude))); } catch (Exception ex) { _loggerService.Error(ex.Message); _loggerService.Verbose("GPS not available."); return(new LocationServiceResponse(true)); } finally { locator?.Dispose(); } } else { _loggerService.Verbose("GPS access not granted."); return(new LocationServiceResponse(false)); } }