none
[UWP]How to define own awaitables RRS feed

  • Question

  • 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.


    Thursday, September 19, 2019 5:01 PM

All replies

  • Hi,

    If you want to asynchronously return a Windows Runtime type, it's better to use IAsyncOperation<TResult>. For more details, you can refer to this document.

    winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Storage::Streams::IBuffer> MainPage::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());

    co_return buffer;
    }

    Best Regards,

    Fay


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, September 20, 2019 9:18 AM
  • In fact, the program runs now through that piece of code (the own async method getBufferAsync). But I am not able to get back the result (buffer). An debug exception is thrown when calling the first async operation (GetfileFromPathAsync) and after calling getBufferAsync.  

    auto a = this->openFile();

    winrt::Windows::Storage::Streams::IBuffer b = a.get();

    winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Storage::Streams::IBuffer> 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); //int i = 0; co_return buffer; } winrt::Windows::Foundation::IAsyncOperation<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()); co_return buffer; }



    • Edited by milonass Tuesday, September 24, 2019 1:31 PM
    Tuesday, September 24, 2019 1:29 PM
  • Hi,

    When you access desktop file, have you added broadFileSystemAccess capability in the manifest and open file access for your app in the Settings? If you have done and still have error, can you please show the detail information about the exception for us?

    Best Regards,

    Fay


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 25, 2019 8:22 AM
  • The app is not an UWP, it is a MFC dialog application. When calling GetFileFromPathAsync I this popup appears:

    Then the following exception is thrown. 


    • Edited by milonass Wednesday, September 25, 2019 4:05 PM
    Wednesday, September 25, 2019 4:05 PM
  • It seems to be a problem with asynchronicity, since the file upload worked before. This behavior occurs since I co_return the IBuffer from my own coroutine. 

    Seems that I really don't understand completely this concept of coroutines. But all the explanations I can find are more or less cryptic. 

     
    Wednesday, September 25, 2019 4:08 PM
  • Hi,

    At first I thought it was a UWP app, and the code works fine in UWP's C++/winrt project. But for MFC applications, my understanding is limited, and MFC applications are not covered by the current UWP forum. Why don't you use the UWP project directly?

    Best Regards,

    Fay


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 26, 2019 6:58 AM
  • Since this is a server application which already exits. I only want to add the OCR capability.

    Regards,

    Thomas

    Thursday, September 26, 2019 10:09 AM
  • Hi,

    MFC application is not the scope of the current forum, we can not provide more help, I suggest you can go to the Visual C++ forum to ask this question.

    Best Regards,

    Fay


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, September 27, 2019 9:07 AM