Wait for it… await, do I have use this with WinRT?

Some people complain that my titles make no sense.  But they do.  To me.  And you right?  After all you are reading this so the title or something got you to this URL.  I think a title like: “Gibberish, followed by the Spanish Inquisition, which no one expected, with a little wonkish speak thrown in” doesn’t make any sense.  Umm, that isn’t a bad title now that I re-read it.

Most of the examples that I have tested that use WInRT use the Asynchronous approach with allows the calling program to continue process while waiting for the C++ code to complete.  However, do you need to use this asynchronous approach?

An excellent blog on the use of WinRT can be found at: http://bit.ly/WinRTParallel

The referenced blog is well written and the example code works!  Note that there is no header file, all of the prototyping is accomplished in the source file.  In the example, there is the use of the await keyword.  See: http://bit.ly/asynchronouscpp

This code construct was initially introduced in C# and Visual Basic, and then added to C++ (or so I have observed, could be wrong). 

When you read these words, you may need some aspirin if you are returning to the one true software language, c++, which might stand for complexity++, but maybe not.  So you use the task class with the concurrency namespace and the ppltasks.h header with the /ZW compiler switch.  It’s that easy.  Ok, if you are hitting this after reading through that C++ book you found in an old books store for 2.50 monetary units, we have a long way to go.  But you do need to use WinRT at sometime, might as well start now.

Although you can consume the asynchronous Windows Runtime APIs directly in C++, the preferred approach is to use the task class and its related types and functions, which are contained in the Concurrency namespace and defined in ppltasks.h. The task class is a general-purpose type, but when the /ZW compiler switch—which is required for Metro style apps and components—is used, the task class encapsulates the Windows Runtime asynchronous types so that it's easier to:

Now you might ask yourself: “Maybe it’s time to switch back to javascript and HTML5”, sure go ahead.  But if you stick with the VC++ stuff you will be able to do both.  SO hang on just a few more lengthy and boring blog posts and you will be one of the top people in the software world according to the oldest person in your family.  Unless that person is me, then you are out of luck. 

All righty now, the follow PARTIAL code (meaning there is no intention that this will work or that I am assuming that you can use this within your own experiments, although I am sure you could!).  Note that this code shows that the prototyping for WinRT can all be done in the Source.CPP, although not a good idea long term in C++, but is nicer if you are converting from Java, C# or VB.  Notice that the public ref class is defined, here is where you would place your asynchronous related code.  Now add a project to your solution, say C#, and then in the appropriate place you would add C# code that refers to the WinRTComponent23, which is the class name, not the namespace (and if you are an experienced programmer, apologies, I know you are aware of that).

The C# code would have a part of code or code snippet: await new WinRTComponent23()

The PARTIAL C++ code is shown here:

// WinRTComponent.cpp
#include <pch.h>
#include <amp.h>
#include <ppltasks.h>
#include <collection.h>
#include <vector>
using namespace concurrency;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
namespace HelloWorldLib
    public ref class WinRTComponent23 sealed
Comments (0)

Skip to main content