await can't find GetAwaitor method
-
The error...
Error 1 'await' requires that the type 'Windows.Foundation.IAsyncOperation' have a suitable GetAwaiter method. Are you missing a using directive for 'System'? C:\Users\jo\Documents\Visual Studio 2013\Projects\Btle01\Btle01\Form1.cs 29 46 Btle01
My code...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Threading.Tasks;
using Windows.Devices.Bluetooth;
using Windows.Devices.Enumeration;
using Windows.Foundation;
using Windows.System;namespace Btle01
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private void OnFormLoad(object sender, EventArgs e) { } private async Task GetDevs() { foreach (DeviceInformation di in await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(BluetoothLEDevice.GetDeviceSelector())) { BluetoothLEDevice bleDevice = await BluetoothLEDevice.FromIdAsync(di.Id); listView1.Items.Add(bleDevice.Name); } } }
}
List of project references...
False
..\..\..\..\..\..\..\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Runtime.WindowsRuntime.dll..\\..\\..\\..\\..\\..\\..\\Windows\\System32\\WinMetadata\\Windows.Devices.winmd
-
The error...
Error 1 'await' requires that the type 'Windows.Foundation.IAsyncOperation' have a suitable GetAwaiter method. Are you missing a using directive for 'System'? C:\Users\jo\Documents\Visual Studio 2013\Projects\Btle01\Btle01\Form1.cs 29 46 Btle01
My code...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Threading.Tasks;
using Windows.Devices.Bluetooth;
using Windows.Devices.Enumeration;
using Windows.Foundation;
using Windows.System;namespace Btle01
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private void OnFormLoad(object sender, EventArgs e) { } private async Task GetDevs() { foreach (DeviceInformation di in await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(BluetoothLEDevice.GetDeviceSelector())) { BluetoothLEDevice bleDevice = await BluetoothLEDevice.FromIdAsync(di.Id); listView1.Items.Add(bleDevice.Name); } } }
}
List of project references...
False
..\..\..\..\..\..\..\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Runtime.WindowsRuntime.dll..\\..\\..\\..\\..\\..\\..\\Windows\\System32\\WinMetadata\\Windows.Devices.winmd
I see that this has been gathering dust for a few days, so I'm sorry if I'm too late to the party to help you.
foreach (DeviceInformation di in await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(BluetoothLEDevice.GetDeviceSelector()))
This is a problem. basically you're attempting to iterate a collection of promises, but without a custom iterator the compiler has no idea what to do with those promises. The easy fix is to move the asynch resolution to a variable and await it, like so:
...
// allow synchronization before iteration
var myArray = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(BluetoothLEDevice.GetDeviceSelector());foreach(DeviceInformation di in myArray)
...Alternatively, you can abuse some of the async functionality in LINQ (I haven't validated this block, so it might need tweaking):
var result = await Task.WhenAll(Windows.Devices.Enumeration.DeviceInformation
.FindAllAsync(BluetoothLEDevice.GetDeviceSelector())
.Select(async x => await BluetoothLEDevice.FromIdAsync(x.Id)).ToArray());result.Select(x => listView1.Items.Add(x.Name));
There are plenty of other ways to skin this cat, should you happen to like cat skins. Both of these approaches should work, though.
-
I see that this has been gathering dust for a few days, so I'm sorry if I'm too late to the party to help you.
foreach (DeviceInformation di in await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(BluetoothLEDevice.GetDeviceSelector()))
This is a problem. basically you're attempting to iterate a collection of promises, but without a custom iterator the compiler has no idea what to do with those promises. The easy fix is to move the asynch resolution to a variable and await it, like so:
...
// allow synchronization before iteration
var myArray = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(BluetoothLEDevice.GetDeviceSelector());foreach(DeviceInformation di in myArray)
...Alternatively, you can abuse some of the async functionality in LINQ (I haven't validated this block, so it might need tweaking):
var result = await Task.WhenAll(Windows.Devices.Enumeration.DeviceInformation
.FindAllAsync(BluetoothLEDevice.GetDeviceSelector())
.Select(async x => await BluetoothLEDevice.FromIdAsync(x.Id)).ToArray());result.Select(x => listView1.Items.Add(x.Name));
There are plenty of other ways to skin this cat, should you happen to like cat skins. Both of these approaches should work, though.