{"id":109957,"date":"2024-07-05T07:00:00","date_gmt":"2024-07-05T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=109957"},"modified":"2024-06-24T09:17:59","modified_gmt":"2024-06-24T16:17:59","slug":"20240705-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20240705-00\/?p=109957","title":{"rendered":"How do I produce a Windows Runtime asynchronous activity from C++\/WinRT?"},"content":{"rendered":"<p>We&#8217;ve looked at how to produce a Windows Runtime asynchronous activity <a title=\"How do I produce a Windows Runtime asynchronous activity from C++\/CX?\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=109953\"> in C++\/CX<\/a> and <a title=\"How\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=109955\"> in C#<\/a>, and they both involved passing a lambda to a helper function, where the lambda received special parameters for detecting cancellation and report progress. Fortunately, C++\/WinRT integrates Windows Runtime asynchrony natively. You just write a function or method that returns an <code>IAsyncSomething<\/code>, and inside the body, you can <code>co_await<\/code> and <code>co_return<\/code>. If you want to check for cancellation, you can request a cancellation token, and if you want to report progress, you can request a progress token.<\/p>\n<pre>winrt::IAsyncOperation&lt;winrt::Widget&gt; GetWidgetAsync(winrt::hstring);\r\nwinrt::IAsyncAction EnableWidgetAsync(winrt::Widget, bool);\r\n\r\nwinrt::IAsyncActionWithProgress&lt;int&gt;\r\n    EnableWidgetByIdAsync(winrt::hstring id, bool enable)\r\n{\r\n    \/\/ If this is an instance member function\r\n    auto lifetime = get_strong();\r\n    \/\/ If this is a global function or static member function\r\n    auto lifetime = wil::winrt_module_reference();\r\n\r\n    <span style=\"border: solid 1px currentcolor; border-bottom: none;\">auto cancel = co_await winrt::get_cancellation_token();<\/span>\r\n    <span style=\"border: solid 1px currentcolor; border-top: none;\">auto progress = co_await winrt::get_progress_token();  <\/span>\r\n\r\n    <span style=\"border: solid 1px currentcolor;\">progress(0);<\/span>\r\n    auto widget = co_await GetWidgetAsync(id);\r\n    <span style=\"border: solid 1px currentcolor; border-bottom: none;\">progress(1);                                  <\/span>\r\n    <span style=\"border: 1px currentcolor; border-style: none solid;\">if (cancel()) throw winrt::hresult_canceled();<\/span>\r\n    <span style=\"border: solid 1px currentcolor; border-top: none;\">if (!widget) co_return false;                 <\/span>\r\n    co_await EnableWidgetAsync(widget, enable);\r\n    co_return true;\r\n}\r\n<\/pre>\n<p>C++\/WinRT automatically checks for cancellation after every <code>co_await<\/code>,\u00b9 so we didn&#8217;t really need a cancellation token in this example, but I showed it for completeness.<\/p>\n<p>\u00b9 Except the special awaitables <code>co_await winrt::<wbr \/>get_<wbr \/>cancellation_<wbr \/>token()<\/code> and <code>co_await winrt::<wbr \/>get_<wbr \/>progress_<wbr \/>token()<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Somebody that deals with them natively.<\/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-109957","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Somebody that deals with them natively.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109957","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=109957"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109957\/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=109957"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=109957"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=109957"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}