Answered by:
Wait for task to finish

Question
-
Hi everybody, I know it has been answered probably a thousand times but still I can't find any answer that fits exactly my problem. I'm a beginner in c sharp and I'm really stuck in this. I'm trying to develop a basic weather forecast app and I use this code to retrieve info about the current GPS location.
class GetCurrentLocation: INotifyPropertyChanged { Geolocator locator = new Geolocator(); private double longtitude; public double Longtitude { get { return longtitude; } } private double latitude; public double Latitude { get { return latitude; } } public LocationInfo CurrentLocationInfo; Task tempTask; Task tempTask2; public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string name) { PropertyChangedEventHandler handler = PropertyChanged; if (handler!=null) { handler(this, new PropertyChangedEventArgs(name)); } } public GetCurrentLocation() //constructor { tempTask = GetCurrentCoordinates(); tempTask2 = SearchLocationByCoordinates(this.longtitude, this.latitude); } public async Task GetCurrentCoordinates() { try { Geoposition position = await locator.GetGeopositionAsync(); longtitude = position.Coordinate.Point.Position.Longitude; latitude = position.Coordinate.Point.Position.Latitude; await SearchLocationByCoordinates(longtitude, latitude); } catch (UnauthorizedAccessException) { ShowMessage("Location service has to to be enabled to get current position"); } } private async void ShowMessage(string message) { MessageDialog dialog = new MessageDialog(message); await dialog.ShowAsync(); } private async Task<Response> GetResponse(Uri uri) { System.Net.Http.HttpClient client = new System.Net.Http.HttpClient(); var response = await client.GetAsync(uri); using (var stream=await response.Content.ReadAsStreamAsync()) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Response)); return ser.ReadObject(stream) as Response; } } public async Task SearchLocationByCoordinates(double longtitude, double latitude) { if (longtitude !=0 && latitude!=0) { string coordinates = latitude.ToString() + "," + longtitude.ToString(); Uri geocodeRequest = new Uri(string.Format("http://dev.virtualearth.net/REST/v1/Locations/{0}?&incl=ciso2&key=1Bing key)); Response r = await GetResponse(geocodeRequest); if (r != null && r.ResourceSets != null && r.ResourceSets.Length > 0 && r.ResourceSets[0].Resources != null && r.ResourceSets[0].Resources.Length > 0) { foreach (BingMapsRESTService.Common.JSON.Location l in r.ResourceSets[0].Resources) { CurrentLocationInfo = new LocationInfo(); CurrentLocationInfo.Longtitude = l.Point.Coordinates[0]; CurrentLocationInfo.Latitude = l.Point.Coordinates[1]; CurrentLocationInfo.Name = l.Name; CurrentLocationInfo.Locality = l.Address.Locality; CurrentLocationInfo.CountryIso2 = l.Address.CountryRegionIso2; } } //Breakpoint here } }
The methods are called from inside another class like this
class LocationManager { List<LocationInfo> LocationList = new List<LocationInfo>(); GetCurrentLocation getCurrentLocation = new GetCurrentLocation(); LocationInfo currentLocationInfo = new LocationInfo(); Task tempTask; Task tempTask2; public LocationManager() //Constractor { tempTask = getCurrentLocation.GetCurrentCoordinates(); tempTask2 = getCurrentLocation.SearchLocationByCoordinates(getCurrentLocation.Longtitude, getCurrentLocation.Latitude); currentLocationInfo = getCurrentLocation.CurrentLocationInfo; }
but in the last line the currentLocationInfo and getCurrentLocation.CurrentLocationInfo return null. When I set a breakpoint at the end of the SearchLocationByCoordinates method I can see that I get results but not when I don't use one. I know that it has to do something with the way I call the methods but due to luck of knowledge I don't know how to solve this. Can anyone help? Thanks in advance.
- Edited by giannisdolon Tuesday, September 30, 2014 5:35 AM
Monday, September 29, 2014 5:45 PM
Answers
-
Hi giannisdolon,
Can I understand your question as you can get a correct location information from SearchLocationByCoordinates() method, but you cannot get a value from getCurrentLocation.CurrentLocationInfo?
I think we can modify the code:
public async Task<LocationInfo> SearchLocationByCoordinates(double longtitude, double latitude) { if (longtitude !=0 && latitude!=0) { string coordinates = latitude.ToString() + "," + longtitude.ToString(); Uri geocodeRequest = new Uri(string.Format("http://dev.virtualearth.net/REST/v1/Locations/{0}?&incl=ciso2&key=Anf_1WGaVZju-4TUUZM421N1WDNbU7hh28lE5QGHm5qfhrmRIMankEt7I3qlC7mF",coordinates)); Response r = await GetResponse(geocodeRequest); if (r != null && r.ResourceSets != null && r.ResourceSets.Length > 0 && r.ResourceSets[0].Resources != null && r.ResourceSets[0].Resources.Length > 0) { foreach (BingMapsRESTService.Common.JSON.Location l in r.ResourceSets[0].Resources) { CurrentLocationInfo = new LocationInfo(); CurrentLocationInfo.Longtitude = l.Point.Coordinates[0]; CurrentLocationInfo.Latitude = l.Point.Coordinates[1]; CurrentLocationInfo.Name = l.Name; CurrentLocationInfo.Locality = l.Address.Locality; CurrentLocationInfo.CountryIso2 = l.Address.CountryRegionIso2; } } //Breakpoint here } return CurrentLocationInfo; } currentLocationInfo = await getCurrentLocation.SearchLocationByCoordinates(getCurrentLocation.Longtitude, getCurrentLocation.Latitude);
--James
<THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
Thanks
MSDN Community Support
Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.- Marked as answer by giannisdolon Tuesday, September 30, 2014 6:47 PM
Tuesday, September 30, 2014 3:32 AMModerator
All replies
-
Hi giannisdolon,
Can I understand your question as you can get a correct location information from SearchLocationByCoordinates() method, but you cannot get a value from getCurrentLocation.CurrentLocationInfo?
I think we can modify the code:
public async Task<LocationInfo> SearchLocationByCoordinates(double longtitude, double latitude) { if (longtitude !=0 && latitude!=0) { string coordinates = latitude.ToString() + "," + longtitude.ToString(); Uri geocodeRequest = new Uri(string.Format("http://dev.virtualearth.net/REST/v1/Locations/{0}?&incl=ciso2&key=Anf_1WGaVZju-4TUUZM421N1WDNbU7hh28lE5QGHm5qfhrmRIMankEt7I3qlC7mF",coordinates)); Response r = await GetResponse(geocodeRequest); if (r != null && r.ResourceSets != null && r.ResourceSets.Length > 0 && r.ResourceSets[0].Resources != null && r.ResourceSets[0].Resources.Length > 0) { foreach (BingMapsRESTService.Common.JSON.Location l in r.ResourceSets[0].Resources) { CurrentLocationInfo = new LocationInfo(); CurrentLocationInfo.Longtitude = l.Point.Coordinates[0]; CurrentLocationInfo.Latitude = l.Point.Coordinates[1]; CurrentLocationInfo.Name = l.Name; CurrentLocationInfo.Locality = l.Address.Locality; CurrentLocationInfo.CountryIso2 = l.Address.CountryRegionIso2; } } //Breakpoint here } return CurrentLocationInfo; } currentLocationInfo = await getCurrentLocation.SearchLocationByCoordinates(getCurrentLocation.Longtitude, getCurrentLocation.Latitude);
--James
<THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
Thanks
MSDN Community Support
Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.- Marked as answer by giannisdolon Tuesday, September 30, 2014 6:47 PM
Tuesday, September 30, 2014 3:32 AMModerator -
It worked that way and I think it's clearer way to write the code that way. Thanks a lot.Tuesday, September 30, 2014 6:49 PM