{"id":103952,"date":"2020-07-03T07:00:00","date_gmt":"2020-07-03T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=103952"},"modified":"2020-07-03T06:43:15","modified_gmt":"2020-07-03T13:43:15","slug":"20200703-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200703-00\/?p=103952","title":{"rendered":"Cancelling a Windows Runtime asynchronous operation, part 3: C++\/CX with PPL, coroutine style"},"content":{"rendered":"<p>Last time, we looked at <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200702-00\/?p=103923\"> how task cancellation is projected in C++\/CX with PPL and explicit continuations<\/a>. But how about C++\/CX with PPL and coroutines?<\/p>\n<pre>auto picker = ref new FileOpenPicker();\r\npicker-&gt;FileTypeFilter.Append(L\".txt\");\r\n\r\n<span style=\"color: blue;\">cancellation_token_source cts;\r\ncall&lt;bool&gt; do_cancel([cts](bool) { cts.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 create_task(picker-&gt;PickSingleFileAsync(), cts.get_token());\r\n} <span style=\"color: blue;\">catch (task_canceled const&amp;)<\/span> {\r\n    file = nullptr;\r\n}\r\n\r\nif (file != nullptr) {\r\n    DoSomething(file);\r\n}\r\n<\/pre>\n<p>Notice that coroutines save us a lot of the hassle of setting up the <code>call<\/code> and <code>timer<\/code> because the objects live in the coroutine frame, which continues to exist until the coroutine completes.<\/p>\n<p>Again, the task throws a <code>task_canceled<\/code> upon cancellation. This time, it&#8217;s because of the <code>await_resume<\/code> for the task awaiter, which you can find in <code>pplawait.h<\/code>:<\/p>\n<pre>template &lt;typename _Ty&gt;\r\nstruct _Ppltask_awaiter {\r\n    ...\r\n\r\n    decltype(auto) await_resume() {\r\n        <span style=\"color: blue;\">return _Task.get();<\/span>\r\n    }\r\n};\r\n<\/pre>\n<p>But wait, the PPL library also supports awaiting on raw <code>IAsyncAction^<\/code> and <code>IAsyncOperation^<\/code> objects. Next time, we&#8217;ll look at what happens in that case.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Same wrapper, different sugar.<\/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-103952","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Same wrapper, different sugar.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103952","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=103952"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103952\/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=103952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=103952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=103952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}