Error calling the asynchronous method at the same time

I am writing code in a desktop application that will be used in police cars to use the new Windows geolocation API. I wrote an event listener for the Geolocator.PositionChanged event, but did not receive the call. When reading the documentation, it seems that Gelocator triggers this event only when the position changes.

I believe that the first thing my program should do after completing the configuration of the event listener is to call the Geolocator.GetPositionAsync method to get the current position, and then allow the position to be updated as they occur.

I need to call the Geolocator.GetPositionAsync method synchronously. To this end, I wrote the following method:

 private async Task<Geoposition> GetCurrentPosition() { await Geolocator.GetGeopositionAsync(); } 

However, I get the following compiler error on a line with await in it:

await requires the type "Windows.Foundation.IAsyncOperation" to have a suitable GetAwaiter method. Do you miss the usage guidelines for the "System"?

Here are the using statements at the top of the file:

 using System; using System.Collections.Generic; using System.ComponentModel; using System.Globalization; using System.Runtime.Serialization; using System.Threading; using System.Threading.Tasks; using System.Xml; using System.Xml.Serialization; using Windows.Devices.Geolocation; 

How to fix this compiler error?

Edit

After adding a reference to the System.Runtime.WindowsRuntime.dll project, the error disappeared. However, now I get a new error:

'System.Runtime.CompilerServices.TaskAwaiter`1 <Windows.Devices.Geolocation.Geoposition>' does not contain a definition for "IsCompleted"

How to fix it?

Edit

Here is the code calling the method:

 protected override void Initialize() { // Make sure we "forget" any previous Gelocator device if ( Geolocator != null ) { Geolocator.PositionChanged -= PositionChanged; Geolocator.StatusChanged -= StatusChanged; Geolocator = null; } CurrentPosition = new GpsInformation() { TimeStamp = DateTime.Now }; Geolocator = new Geolocator(); Geolocator.DesiredAccuracy = PositionAccuracy.High; // Sends position updates with the highest accuracy possible. Geolocator.MovementThreshold = 0; // Sends position updates no matter how far the vehicle has moved since the last update. Geolocator.ReportInterval = 0; // Sends position updates as they become availble. Geolocator.PositionChanged += PositionChanged; // Sends position updates to this method. Geolocator.StatusChanged += StatusChanged; // Sends status changed updates to this method. switch ( Geolocator.LocationStatus ) { case PositionStatus.Disabled: case PositionStatus.NotAvailable: // The Geolocator device is disabled. We won't get any updates. Throw an exception. throw new Exception( "LPRCore does not have permission to use Windows Geolocation services or Geolocation services are not working." ); } Task<Geoposition> getPositionTask = GetCurrentPosition(); positionRecord = getPositionTask.Result; } 
0
source share
3 answers

Thank you Scott for your help. I was able to figure out which DLL files to add links from your sentences, and then try or try to try.

To fix the problem, I needed to add links to:

  • System.Threading.dll and
  • System.Threading.Tasks.dll

As soon as I did this, the error disappeared.

I want MS to add information about which DLLs should reference the use of the Windows 8 API from VS2012 in their documentation.

+4
source

A possible duplicate question, but you should also add a link to System.Runtime.WindowsRuntime.dll, if you have not already done so.

+1
source

I think you ran into this problem trying to solve the wrong problem.

During the investigation, to write Providing the geolocation as a reactive service on the Nokia Developer Wiki I encountered the same problem (no change) and tried the same solution (calling Geolocator.GetGeopositionAsync ).

Turns out you need to set some properties ( Geolocator.MovementThreshold , Geolocator.DesiredAccuracy , Geolocator.ReportInterval ) with some values.

If you are not familiar with Reactive Extensions (Rx) , you should try. You can get my implementation from my CodeProject workspace .

+1
source

All Articles