Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

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;
});

Enjoy!

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:

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

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}