How to define own awaitables

  • Thread starter Thread starter milonass
  • Start date Start date
M

milonass

Guest
I have the following code

winrt::Windows::Foundation::IAsyncAction COCRServer1Dlg::openFile()
{
//winrt::Windows::Storage::StorageFolder storageFolder{ winrt::Windows::Storage::ApplicationData::Current().LocalFolder() };
//winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Storage::StorageFile> fileOp = storageFolder.GetFileAsync(L"C:\\Users\\Thomas\\Desktop\\test.bmp");

auto file = co_await winrt::Windows::Storage::StorageFile::GetFileFromPathAsync(L"C:\\Users\\Thomas\\Desktop\\test.bmp");

auto stream = co_await file.OpenReadAsync();

auto BMDecoder = co_await winrt::Windows::Graphics::Imaging::BitmapDecoder::CreateAsync(stream);

auto BMI = co_await BMDecoder.GetSoftwareBitmapAsync();

auto buffer = co_await getBufferAsync(BMI);

co_return;

}

concurrency::task<winrt::Windows::Storage::Streams::IBuffer> COCRServer1Dlg::getBufferAsync(winrt::Windows::Graphics::Imaging::SoftwareBitmap BM)
{
winrt::Windows::UI::Xaml::Media::Imaging::WriteableBitmap wb = winrt::Windows::UI::Xaml::Media::Imaging::WriteableBitmap(BM.PixelWidth(), BM.PixelHeight());

winrt::Windows::Storage::Streams::IBuffer buffer = static_cast<winrt::Windows::Storage::Streams::IBuffer>(wb.PixelBuffer());

return concurrency::task_from_result<winrt::Windows::Storage::Streams::IBuffer>(buffer);
}


All the code works until the call of getBufferAsync. This non-static member function is a task and should be (in my opinion) a valid awaitable. I can call it with operator co_await. But the statements in the function are never called. Obviously I didn't understand correctly how to define a coroutine. On the other hand, when I try to execute the statements synchronously in openFile, they are also ignored.

Thank you.

Continue reading...
 
Back
Top