{"id":103960,"date":"2020-07-07T07:00:00","date_gmt":"2020-07-07T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=103960"},"modified":"2020-07-07T08:04:53","modified_gmt":"2020-07-07T15:04:53","slug":"20200707-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200707-00\/?p=103960","title":{"rendered":"Cancelling a Windows Runtime asynchronous operation, part 5: C++\/WinRT"},"content":{"rendered":"<p>Last time, we looked at <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200706-00\/?p=103958\"> how task cancellation is projected in C++\/CX with PPL and <code>co_await<\/code> with <code>IAsyncAction^<\/code> and <code>IAsyncOperation^<\/code> objects<\/a>. Our next investigation is C++\/WinRT.<\/p>\n<pre>FileOpenPicker openPicker;\r\nopenPicker.FileTypeFilter().Append(L\".txt\");\r\nauto pickerOp = openPicker.PickSingleFileAsync();\r\n<span style=\"color: blue;\">([](auto op) -&gt; fire_and_forget { co_await resume_after(3s); op.Cancel(); })(pickerOp);<\/span>\r\n\r\nStorageFile file{ nullptr };\r\ntry\r\n{\r\n    file = co_await pickerOp;\r\n}\r\ncatch (<span style=\"color: blue;\">hresult_canceled const&amp;<\/span>)\r\n{\r\n    file = nullptr;\r\n}\r\ncatch (<span style=\"color: blue;\">hresult_illegal_method_call const&amp;<\/span>)\r\n{\r\n    file = nullptr;\r\n}\r\n\r\nif (file != nullptr)\r\n{\r\n    DoSomething(file);\r\n}\r\n<\/pre>\n<p>Canceling the operation after a delay is slightly tricky because we need to pass the <code>pickerOp<\/code> as a parameter to a captureless lambda, rather than capturing it into the lambda. We discussed the reason for this <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20190117-00\/?p=100725\"> some time ago<\/a>.<\/p>\n<p>The exception that comes out of awaiting for a canceled task is sometimes an <code>hresult_<wbr \/>canceled<\/code>, and sometimes an <code>hresult_<wbr \/>illegal_<wbr \/>method_call<\/code>. The reason is that C++\/WinRT defers to the <code>IAsyncAction<\/code>\/<wbr \/><code>IAsyncOperation<\/code> to decide what exception to raise.\u00b9<\/p>\n<p>In other words, C++\/WinRT just takes the ABI result and propagates it. It doesn&#8217;t try to impose its will upon the result.<\/p>\n<p>You can see this in the <code>await_<wbr \/>resume<\/code> for asynchronous operations:<\/p>\n<pre>template &lt;typename Async&gt;\r\nstruct await_adapter\r\n{\r\n    ...\r\n\r\n    auto await_resume() const\r\n    {\r\n        return async.GetResults();\r\n    }\r\n};\r\n<\/pre>\n<p>Whatever exception comes out of <code>Get\u00adResults()<\/code> is the exception that comes out of the <code>co_await<\/code>.<\/p>\n<p>Next time, we&#8217;ll look at what happens if the <code>IAsyncAction<\/code> or <code>IAsyncOperation<\/code> was implemented by the C++\/WinRT library itself.<\/p>\n<p><b>Bonus chatter<\/b>: This entire article is already obsolete. We&#8217;ll learn more about it when this series wraps up.<\/p>\n<p>\u00b9 Therefore, in principle, it could be <i>utterly anything<\/i> since it&#8217;s up to the ABI to generate the <code>HRESULT<\/code> that turns into an exception at the projection.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Relying on the ABI result.<\/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-103960","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Relying on the ABI result.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103960","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=103960"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103960\/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=103960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=103960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=103960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}