我有点绝望地试图获得带有Windows::Foundation::IAsyncOperation
接口的 winrt 异步方法的结果。该项目是启用了 winRT 扩展的 Visual Studio Community 2017 c++ 项目。我试过使用std::future
/co_await
函数和任务方法,但调用GetResults()
总是生成“在意外时间调用”异常。
使用以下代码,我没有得到异常但GetResults()
返回nullptr
。我也尝试在代码中声明async_op
为 ashared_ptr
auto async_op = std::make_shared<Windows::Foundation::IAsyncOperation<Windows::Devices::Bluetooth::BluetoothLEDevice^> ^>
并访问它,但我得到了相同的结果。*async_op
Handler
欢迎任何想法。
提前致谢。
void BleEnumeration::PerformConnectDevice(std::string* bleDevId)
{
std::wstring bleDevId_w_str = std::wstring((*bleDevId).begin(), (*bleDevId).end());
const wchar_t* bleDevId_w_char = bleDevId_w_str.c_str();
Platform::String^ bleDevId_refStr = ref new Platform::String(bleDevId_w_char);
auto async_op = Windows::Devices::Bluetooth::BluetoothLEDevice::FromIdAsync(bleDevId_refStr);
async_op->Completed = ref new Windows::Foundation::AsyncOperationCompletedHandler<Windows::Devices::Bluetooth::BluetoothLEDevice^>(
[async_op] (Windows::Foundation::IAsyncOperation< Windows::Devices::Bluetooth::BluetoothLEDevice^ >^ operation, Windows::Foundation::AsyncStatus status)
{
if (async_op->Status == Windows::Foundation::AsyncStatus::Completed)
{
auto bleDevTest = async_op->GetResults();
}
});
}