{"id":106253,"date":"2022-02-15T07:00:00","date_gmt":"2022-02-15T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=106253"},"modified":"2022-02-15T08:43:44","modified_gmt":"2022-02-15T16:43:44","slug":"20220215-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20220215-00\/?p=106253","title":{"rendered":"COM asynchronous interfaces, part 2: Abandoning the operation"},"content":{"rendered":"<p>Last time, we <a title=\"COM asynchronous interfaces, part 1: The basic pattern\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20220214-44\/?p=106251\"> ran a very basic demonstration of COM asynchronous interfaces<\/a>. Now we&#8217;ll start to get fancy.<\/p>\n<p>Our first fancy thing is creating a fire-and-forget asynchronous call. To do this, you simply abandon the call object after calling the <code>Begin_<\/code> method. It means that you never find out the answer, or even learn that it failed. But if you were going to ignore the result anyway, then it doesn&#8217;t matter.<\/p>\n<pre>int main(int, char**)\r\n{\r\n  winrt::init_apartment(winrt::apartment_type::multi_threaded);\r\n\r\n  auto pipe = CreateSlowPipeOnOtherThread();\r\n\r\n  winrt::com_ptr&lt;::AsyncIPipeByte&gt; call;\r\n  auto factory = pipe.as&lt;ICallFactory&gt;();\r\n  winrt::check_hresult(factory-&gt;CreateCall(\r\n    __uuidof(::AsyncIPipeByte), nullptr,\r\n    __uuidof(::AsyncIPipeByte),\r\n    reinterpret_cast&lt;::IUnknown**&gt;(call.put())));\r\n\r\n  <span style=\"color: blue;\">printf(\"Beginning the Push\\n\");\r\n  BYTE buffer[15] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,\r\n                      11, 12, 13, 14, 15 };\r\n  winrt::check_hresult(call-&gt;Begin_Push(buffer, 15));\r\n\r\n  call = nullptr; \/\/ throw it away!\r\n  printf(\"Abandoned the call\\n\");\r\n\r\n  Sleep(5000); \/\/ just so you can see the other thread<\/span>\r\n  return 0;\r\n}\r\n<\/pre>\n<p>After initiating a <code>Push<\/code> with <code>Begin_<wbr \/>Push<\/code>, we simply release the call object (which happens when we set it to <code>nullptr<\/code>) and go on with our lives. The call is still running, but we have lost our ability to access its results.<\/p>\n<p>If you run this program, you can see that the call to <code>Push<\/code> continues running to completion. Depending on the timing, you may even observe the call become abandoned <i>before<\/i> the <code>Push<\/code> method even starts!<\/p>\n<p>Next time, we&#8217;ll abandon the operation only if it takes too long.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Simply losing interest.<\/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-106253","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Simply losing interest.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/106253","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=106253"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/106253\/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=106253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=106253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=106253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}