Over a million developers have joined DZone.

Asynchronous Loading Text Files in Metro/C++/DirectX Apps

DZone's Guide to

Asynchronous Loading Text Files in Metro/C++/DirectX Apps

· Mobile Zone
Free Resource

Discover how to focus on operators for Reactive Programming and how they are essential to react to data in your application.  Brought to you in partnership with Wakanda

By just adding a simple modification to the existing ReadDataAsync function, we can load text files asynchronously:

inline Concurrency::task<Platform::String^> ReadStringAsync(Platform::String^ filename)
using namespace Windows::Storage;
using namespace Concurrency;

auto folder = Windows::ApplicationModel::Package::Current->InstalledLocation;

task<StorageFile^> getFileTask(folder->GetFileAsync(filename));

auto readBufferTask = getFileTask.then([] (StorageFile^ f)
return FileIO::ReadBufferAsync(f);

auto byteArrayTask = readBufferTask.then([] (Streams::IBuffer^ b) -> Platform::String^
return Streams::DataReader::FromBuffer(b)->ReadString(b->Length);

return byteArrayTask;

You can load files using the following code (don’t forget it is asynchronous.

auto loadTextTask = ReadStringAsync(L"Assets\\Text.txt")
                                   .then([this](Platform::String^ text)
    m_text = text;


Learn how divergent branches can appear in your repository and how to better understand why they are called “branches".  Brought to you in partnership with Wakanda


Published at DZone with permission of Toni Petrina, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}