Page 47 - MSDN Magazine, June 2018
P. 47

You can, of course, use the C++ standard library’s mutex and condition variable if you prefer. The point here is simply that the Completed handler is your hook to wiring up async completion and it can be done quite generically.
Naturally, there’s no reason for you to write your own get function, and more than likely coroutines will be much simpler and more versatile in general. Still, I hope this helps you appreciate some of the power and flexibility in the Windows Runtime.
Producing Async Objects
Now that we’ve explored the async interfaces and some completion mechanics in general, let’s turn our attention to creating or pro- ducing implementations of those four async interfaces. As you’ve already learned, implementing WinRT interfaces with C++/WinRT is very simple. I might, for example, implement IAsyncAction, as shown in Figure 5.
The difficulty comes when you consider how you might implement those methods. While it’s not hard to imagine some implemen- tation, it’s almost impossible to do this correctly without first reverse-engineering how the existing language projections actually implement them. You see, the WinRT async pattern only works if everyone implements these interfaces using a very specific state machine and in exactly the same way. Each language projection makes the same assumptions about how this state machine is implemented and if you happen to implement it in a slightly dif- ferent way, bad things will happen.
Thankfully, you don’t have to worry about this because each language projection, with the exception of C++/CX, already implements this correctly for you. Here’s a complete implementa- tion of IAsyncAction thanks to C++/WinRT’s coroutine support:
IAsyncAction CopyAsync() {
co_return; }
Now this isn’t a particularly interesting implementation, but it is very educational and a good example of just how much C++/ WinRT is doing for you. Because this is a complete implementa- tion, we can use it to exercise some of what we’ve learned thus far. The earlier CopyAsync function is a coroutine. The coroutine’s return type is used to stitch together an implementation of both IAsyncAction and IAsyncInfo, and the C++ compiler brings it to life at just the right moment. We’ll explore some of those details later, but for now let’s observe how this coroutine works. Consider the following console app:
IAsyncAction CopyAsync() {
co_return; }
int main() {
IAsyncAction async = CopyAsync();
async.get(); }
The main function calls the CopyAsync function, which returns an IasyncAction. If you forget for a moment what the CopyAsync function’s body or definition looks like, it should be evident that it’s just a function that returns an IAsyncAction object. You can there- fore use it in all the ways that you’ve already learned. msdnmagazine.com
Seeking for professional and easy-to-use Imaging .NET SDK?
VintaSoft Imaging .NET SDK
Load, view, convert, manage, print, capture from camera, save raster images and PDF documents.
Image Viewer for .NET, WPF and WEB
100+ Image Processing and Document Cleanup commands
PDF Reader, Writer, Visual Editor Image Annotations
JBIG2 and JPEG2000 codecs OCR and Document Recognition Forms Processing and OMR DICOM decoder
Barcode Reader and Generator TWAIN scanning
Free evaluation version
Royalty free licensing
www.vintasoft.com VintaSoft is the registered trademark
of VintaSoft Ltd.


































































































   45   46   47   48   49