[UWP]如何为Windows运行时API编写简单的协程(C ++ / winRT UWP) [英] [UWP]How to write a simple coroutine (C++/winRT UWP) for windows runtime APIs

查看:68
本文介绍了[UWP]如何为Windows运行时API编写简单的协程(C ++ / winRT UWP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了避免UI线程断言错误,我需要异步访问BluetoothAdapter对象。 如果我只能理解文档,协程应该解决这个问题。深奥的语法确实使得这个过程更加困难。


同步方法如下:

 IAsyncOperation< BluetoothAdapter> async = BluetoothAdapter :: GetDefaultAsync(); 
BluetoothAdapter bluetoothAdapter = async.get();

我查看了这个博客  https://msdn.microsoft.com/en-us/magazine/mt846728.aspx?f=255&MSPPError= -2147217396


b 我尝试过的一切都失败了。这可能是因为该文章没有处理已经是异步类型的运行时API,它可能涉及一些我想不通的
特殊语法。我尝试了以下内容,结果就好像没有co_await或co_return(只是按照上面的方式直接阻塞get():


这是我的尝试:

 bool WindowsBluetoothAdapter :: isBluetoothLESupported()
{
if(!isBluetoothLESupportedSet)
{
BluetoothAdapter bluetoothAdapter = GetBluetoothAdapter( ).get();

bluetoothLESupported = bluetoothAdapter.IsLowEnergySupported();
isBluetoothLESupportedSet = true;
}
返回bluetoothLESupported;
}

IAsyncOperation< BluetoothAdapter> WindowsBluetoothAdapter :: GetBluetoothAdapter()
{
BluetoothAdapter bluetoothAdapter {co_await BluetoothAdapter :: GetDefaultAsync()};
co_return bluetoothAdapter;
}

我不确定bluetoothAdapter之后的{}语法是什么,但它是VS 2019唯一接受的东西。它让我想起了Java内联类。

解决方案

这是
https://docs.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/concurrency#write-a-coroutine
 帮助

To avoid a UI thread assertion error I need to access the BluetoothAdapter object asynchronously.  A coroutine should solve this if I could only understand the documentation. The esoteric syntax does make the process more difficult.

The synchronous approach is as follows:

IAsyncOperation<BluetoothAdapter> async = BluetoothAdapter::GetDefaultAsync();
BluetoothAdapter bluetoothAdapter = async.get();

I looked at this blog https://msdn.microsoft.com/en-us/magazine/mt846728.aspx?f=255&MSPPError=-2147217396

but everything I tried failed. That's probably because the article did not deal with runtime APIs that are already Async types and it probably involves some special syntax I cannot figure out. I have tried the following and the result is as if there was no co_await or co_return (just doing a straight blocking get() as above:

Here is my attempt:

bool WindowsBluetoothAdapter::isBluetoothLESupported()
{
	if (!isBluetoothLESupportedSet)
	{
		BluetoothAdapter bluetoothAdapter = GetBluetoothAdapter().get();

		bluetoothLESupported = bluetoothAdapter.IsLowEnergySupported();
		isBluetoothLESupportedSet = true;
	}
	return bluetoothLESupported;
}

IAsyncOperation<BluetoothAdapter> WindowsBluetoothAdapter::GetBluetoothAdapter()
{
	BluetoothAdapter bluetoothAdapter{ co_await BluetoothAdapter::GetDefaultAsync() };
	co_return bluetoothAdapter;
}

I am not sure what the {} syntax does after the bluetoothAdapter, but its the only thing VS 2019 would accept. It reminds me of a Java inline class.

解决方案

Does this https://docs.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/concurrency#write-a-coroutine  help?


这篇关于[UWP]如何为Windows运行时API编写简单的协程(C ++ / winRT UWP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆