I’m working with the patterns & practices team on a Metro style app with C++ and XAML called Hilo. Hilo is a basic photo viewing and tagging app that totally re-imagines Hilo for Windows 7 by embracing new form factors and experiences that Windows 8 enables.
One of the more exciting aspects of Hilo is the use of the new PPL tasks to enable a fast and fluid user experience. The heart of PPL tasks, the concurrency::task class, simplifies the process of performing operations asynchronously, or in the background, and then using the results of the asynchronous operations to update the UI.
For example, Hilo defines the ThumbnailGenerator class to generate thumbnails for pictures that are displayed on the app’s Start menu tile. The ThumbnailGenerator::Generate queues a task that converts each image in the provided vector to a thumbnail that is displayed on the tile. The call to concurrency::when_all enables this method to collect the results when they are ready and produce a second vector that contains the resulting thumbnail images.
1: task<Platform::Collections::Vector<StorageFile^>^> ThumbnailGenerator::Generate(
2: IVector<StorageFile^>^ files,
3: StorageFolder^ thumbnailsFolder)
5: vector<task<StorageFile^>> thumbnailTasks;
7: unsigned int imageCounter = 0;
8: for_each(begin(files), end(files),
9: [&imageCounter, thumbnailsFolder, &thumbnailTasks](StorageFile^ imageFile)
11: wstringstream localFileName;
12: localFileName << ThumbnailImagePrefix << imageCounter++ << ".jpg";
18: ref new String(localFileName.str().c_str()),
22: return when_all(begin(thumbnailTasks), end(thumbnailTasks)).then(
23: (vector<StorageFile^> files)
25: auto result = ref new Platform::Collections::Vector<StorageFile^>();
26: for_each(begin(files), end(files), [result](StorageFile^ file)
28: if (file != nullptr)
33: return result;
This method behaves asynchronously. The caller to this method receives a task object and can then chain additional tasks that will run after that task completes.
Here’s another example. This one creates a chain of continuations to save the thumbnail image to disk. The ThumbnailGenerator::InternalSaveToFile method is called by ThumbnailGenerator::CreateLocalThumbnailAsync in the previous example.
1: task<StorageFile^> ThumbnailGenerator::InternalSaveToFile(
2: StorageFolder^ thumbnailsFolder,
3: InMemoryRandomAccessStream^ stream,
4: Platform::String^ filename)
6: auto imageFile = make_shared<StorageFile^>(nullptr);
7: auto streamReader = ref new DataReader(stream);
8: task<unsigned int> loadStreamTask(
9: streamReader->LoadAsync(static_cast<unsigned int>(stream->Size)));
11: return loadStreamTask.then(
12: [thumbnailsFolder, filename](unsigned int loadedBytes)
14: return thumbnailsFolder->CreateFileAsync(
18: }).then([streamReader, imageFile](StorageFile^ thumbnailDestinationFile)
20: (*imageFile) = thumbnailDestinationFile;
21: auto buffer = streamReader->ReadBuffer(
24: return FileIO::WriteBufferAsync(thumbnailDestinationFile, buffer);
28: return (*imageFile);
Although this method is called as part of another asynchronous operation, we got in the habit of always using asynchronous operations so that we can later compose them together. This is key to keeping work off of the UI thread, and thus keeping the app responsive to user input.
We also strive to use lambda functions, standard algorithms, the auto keyword, and other modern C++ constructs as much as possible.
Check out Hilo for Windows 8 at http://hilo.codeplex.com. You’ll need Windows 8 Consumer Preview and Microsoft Visual Studio 11 Express Beta for Windows 8 to run the sample.
This project is a work in progress. Try it today and be sure to join us on the Discussions tab. We’d love to hear your feedback on our progress!