Using resumable functions in the CxxReadFileComponent sample

Hello,

Welcome back from holidays. A few weeks ago, I posted a note about updating the samples of my book to the November CTP of Visual C++. The main goal behind the update is to move away from using PPL tasks for WinRT asynchronous programming and instead use the new resumable functions available in the CTP.

Today is the beginning! The first sample I will talk about updating is the CxxReadFileComponent sample that discusses about building a C++ WinRT Component that can be consumed from other WinRT supported languages.

Before we begin:

  1. Get the updated sample here. We will use this sample as a starting point for our migration. Before we write actual code, it is important to note that the CTP is only supported along with Microsoft Visual Studio 2013.
  2. The CTP works with VS 2013. In case you do not have VS 2013, you can get either an express (free) edition or a trial edition here.
  3. In case you have not already downloaded the Visual C++ November CTP, please do so. You can download and install the CTP from this location.
  4. Install VS 2013 first and then the CTP.
  5. Time for action!!!

The first step is to migrate the solution from Microsoft Visual Studio 2012 to the 2013 version.

When you attempt to open the solution file in Visual Studio 2013, Visual Studio prompts you to retarget the solution for Windows 8.1 as shown in the dialog below. If you choose not to retarget, then you will have to install the Windows 8 development tools in order to build the solution.


For the purpose of this exercise, go ahead and retarget the solution to Windows 8.1 by first clicking on OK in the above dialog and then right click on the project in Solution Explorer and choose Retarget to Windows 8.1

Once you ask Visual Studio to retarget the solution to Windows 8.1, Visual Studio makes the necessary changes and opens the solution. Once the solution is fully loaded, right click on the CxxReadFileComponent project in Solution Explorer and choose Properties. In the Properties Dialog, under the Platform Toolset, choose Visual C++ Compiler Nov 2013 CTP (CTP_Nov2013) and click on OK.

The CxxReadFileComponent has one class named ReadFile. You can find the relevant code in the wrongly named files, Class1.h and Class1.cpp. The ReadFile class defines public methods that returns one of the IAsync* interfaces that all WinRT asynchronous methods are expected to return. This allows for consumption of such async operations from other languages. Before we go ahead with changing code, let’s open pch.h and add a new reference for a header. Add a new include reference for pplawait.h in pch.h which pulls in all the required declarations necessary for the new resumable functions.

Things to remember when using resumable functions in C++ for WinRT

  1. You cannot have resumable support for any WinRT method that returns one of the IAsync* interfaces.
  2. Only methods that return either a Task<T> or void can be resumable.

Making code changes

Now, it is time to make changes to code. Open Class1.cpp and navigate to the ReadBytes function. The original function is listed below:

Original ReadBytes function using PPL tasks:

You can see from the code above, using tasks and chaining them can become quite “messy”. With the new resumable functions, each task can be replaced with a single call to the underlying async API along with the __await keyword prepended to the API call. That’s it. No lambdas, no chaining etc.

Updated ReadBytes function using resumable functions:

task<UINT> ReadFile::ReadBytes(String^ fileName) __resumable

{

    StorageFolder^ item = KnownFolders::DocumentsLibrary;

    auto storageFile = __await item->GetFileAsync(fileName);

    auto istream = __await storageFile->OpenAsync(FileAccessMode::Read);

    auto reader = ref new DataReader(istream);

    return __await reader->LoadAsync(static_cast<UINT>(istream->Size));

}

Disclaimer: I have not added error handling. You can add proper if- checks and handle errors. The intention here is to show the ease of use of the resumable functions.

Changes made:

  1. All resumable methods have a __resumable identifier added at the end of the function declaration in both the header and the source files.
  2. Use the __await keyword when making calls to the WinRT Async APIs.
  3. That’s it!!

Similarly I made changes to the ReadBytesWithProgress function. The original function is listed below:

Original ReadBytesWithProgress function using PPL tasks:

Updated ReadBytesWithProgress function using resumable functions:

task<UINT> ReadFile::ReadBytesWithProgress(Platform::String^ fileName, Concurrency::progress_reporter<double> progress) __resumable

{

    StorageFolder^ item = KnownFolders::DocumentsLibrary;

    auto storageFile = __await item->GetFileAsync(fileName);

    progress.report(25.0);

    auto istream = __await storageFile->OpenAsync(FileAccessMode::Read);

    progress.report(50.0);

    auto reader = ref new DataReader(istream);

    progress.report(75.0);

    progress.report(100.0);

    return __await reader->LoadAsync(static_cast<UINT>(istream->Size));

}

With these changes made, build the solution and run the C# client test app. Make sure you have a text file named 127.txt in your Documents folder before you run the test app. Once you run the app, you can see that the app makes calls into your component and reads the number of bytes in the file using the updated methods, which really simplify writing asynchronous code using C++.

That’s it for today. See you soon for a next sample update. Stay tuned as this is getting very exciting!!

-Sridhar

2 Comments

  1. Hatim Haidry

    Hi Sridhar,

    Its a very nice article shared by you.

    And the same I am trying to implement in my C++ CLI code and compiling with CLR option And getting an error during compilation as “C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\atomic(9): fatal error C1189: #error : is not supported when compiling with /clr or /clr:pure”

    Please help.

    Thanks & Regards
    Hatim

    • Hi Hatim, You cannot use the reumable functions in C++/CLI or code compiled with the CLR option turned ON. The resumable functions work only with native code.

      HTH,
      Sridhar

Comments are closed