{"id":103958,"date":"2020-07-06T07:00:00","date_gmt":"2020-07-06T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=103958"},"modified":"2020-07-06T07:22:19","modified_gmt":"2020-07-06T14:22:19","slug":"20200706-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200706-00\/?p=103958","title":{"rendered":"Cancelling a Windows Runtime asynchronous operation, part 4: C++\/CX with PPL, coroutine style with raw IAsyncAction and IAsyncOperation"},"content":{"rendered":"<p>Last time, we looked at <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200703-00\/?p=103952\"> how task cancellation is projected in C++\/CX with PPL and <code>co_await<\/code> with tasks<\/a>. But PPL also supports awaiting directly on <code>IAsyncAction^<\/code> and <code>IAsyncOperation^<\/code> objects. Let&#8217;s try it.<\/p>\n<pre>auto picker = ref new FileOpenPicker();\r\npicker-&gt;FileTypeFilter.Append(L\".txt\");\r\n\r\n<span style=\"color: blue;\">auto pickerOp = picker-&gt;PickSingleFileAsync();\r\ncancellation_token_source cts;\r\ncall&lt;bool&gt; do_cancel([pickerOp](bool) { pickerOp.Cancel(); });\r\ntimer&lt;bool&gt; delayed_cancel(3000U, false, &amp;do_cancel);\r\ndelayed_cancel.start();<\/span>\r\n\r\nStorageFile^ file;\r\ntry {\r\n    file = co_await pickerOp;\r\n} <span style=\"color: blue;\">catch (OperationCanceledException^)<\/span> {\r\n    file = nullptr;\r\n}\r\n\r\nif (file != nullptr) {\r\n    DoSomething(file);\r\n}\r\n<\/pre>\n<p>Observe that awaiting directly on an <code>IAsyncAction^<\/code> and <code>IAsyncOperation^<\/code> object throws a different exception from awaiting on a <code>task<\/code>.<\/p>\n<p>You can see this in the <code>await_resume<\/code> for the <code>IAsyncInfo^<\/code> awaiter ind in <code>pplawait.h<\/code>:<\/p>\n<pre>template &lt;typename _TaskTy, typename _HandlerTy&gt;\r\nstruct _IAsync_awaiter {\r\n    ...\r\n\r\n    auto await_resume() {\r\n        <span style=\"color: blue;\">_VerifyStateForResultsCall(_Task-&gt;Status);<\/span>\r\n        return _Task-&gt;GetResults();\r\n    }\r\n};\r\n\r\nvoid _VerifyStateForResultsCall(\r\n    Windows::Foundation::AsyncStatus _Status)\r\n{\r\n    if (_Status == AsyncStatus::Canceled) {\r\n        throw ::Platform::Exception::CreateException(E_ABORT);\r\n    }\r\n}\r\n<\/pre>\n<p>The PPL framework checks whether the status of the async operation is <code>Canceled<\/code>, and if so, it throws <code>E_ABORT<\/code>, which is represented in C++\/CX as <code>Operation\u00adCanceled\u00adException<\/code>.<\/p>\n<p>So far, all of the cancellation exceptions have generated by the framework. That&#8217;ll change soon. Next time, we&#8217;ll look at C++\/WinRT.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Looks the same, but actually tastes completely different.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-103958","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Looks the same, but actually tastes completely different.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103958","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=103958"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103958\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=103958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=103958"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=103958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}