Однако я обнаружил, что на некоторых компьютерах (!) Задача висит, но через 20 секунд Task.delay () не завершен (под которым я имею в виду, что он не переходит в раздел «/Код»>, он просто висит Форвер. На планшете с Windows 10 22H2 это не так! < /p>
Код: Выделить всё
GattDeviceServicesResult services = null;
var task = bleDevice.GetGattServicesAsync(BluetoothCacheMode.Uncached).AsTask();
if (await Task.WhenAny(task, Task.Delay(20000)) == task)
{
Console.WriteLine($"Found Services for device:")
services = task.Result;
if (services.Services != null)
{
foreach (var service in services.Services)
{
Console.WriteLine($"Service: {service.Uuid}");
}
}
}
else
{
Console.WriteLine("Timed Out while while getting service list.");
}
< /code>
AsTask()Код: Выделить всё
public static Task AsTask(this IAsyncOperation operation)
{
// Create task completion result.
var tcs = new TaskCompletionSource();
// When the operation is completed...
operation.Completed += delegate
{
switch(operation.Status)
{
case AsyncStatus.Completed:
tcs.TrySetResult(operation.GetResults());
break;
case AsyncStatus.Error:
tcs.TrySetException(operation.ErrorCode);
break;
case AsyncStatus.Canceled:
tcs.SetCanceled();
break;
}
};
return tcs.Task;
}
< /code>
... but it's the same if I used the AsTask()Подробнее здесь: https://stackoverflow.com/questions/794 ... timing-out
Мобильная версия