{"id":103610,"date":"2020-03-27T07:00:00","date_gmt":"2020-03-27T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=103610"},"modified":"2020-03-26T19:12:25","modified_gmt":"2020-03-27T02:12:25","slug":"20100327-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200327-00\/?p=103610","title":{"rendered":"Are Windows Runtime asynchronous operations guaranteed to complete?"},"content":{"rendered":"<p>The Windows Runtime uses asynchronous operations, which are operations which start and return immediately, and then notify you when the operation has completed. This lets you do other things while waiting for the operation. Most programming languages nowadays have built-in support for this style of programming, usually by using some variation of the keyword <code>await<\/code>.<\/p>\n<p>Is there any guarantee that a Windows Runtime operation will eventually complete?<\/p>\n<p>Is there any guarantee that <i>any<\/i> operation will eventually complete?<\/p>\n<p>Not really.<\/p>\n<p>For example, you might display a dialog box to the user by calling <code>MessageBox.ShowAsync<\/code>. This completes when the user responds to the dialog. But what if the user isn&#8217;t there? The dialog box remains on screen indefinitely. Now, it&#8217;s possible that the user might return someday, so you might argue that the operation hasn&#8217;t definitely gotten stuck, because the user can always unstick it by responding to the dialog box.<\/p>\n<p>The <code>AnimatedVisualPlayer.PlayAsync<\/code> method completes when the animation stops. This happens naturally if you ask the animation to play to the end and stop, but if you ask for a looping animation, then it doesn&#8217;t stop until you manually call <code>Stop<\/code> to stop it. Does this mean that there&#8217;s no guarantee that the <code>PlayAsync<\/code> will ever complete? I mean, your program can always unstick it by calling <code>Stop<\/code>.<\/p>\n<p>Each asynchronous operation defines the conditions under which it will complete. If those conditions are never met, then it will never complete. There&#8217;s nothing special about asynchronous operations here. This can happen with synchronous functions, too. If you ask <code>Wait\u00adFor\u00adSingle\u00adObject<\/code> to wait for a handle that will never be signaled, then it will never return.<\/p>\n<p><b>Bonus chatter<\/b>: You can easily create your own Windows Runtime asynchronous operation that never completes.<\/p>\n<pre>winrt::IAsyncAction HangAsync()\r\n{\r\n    co_await std::experimental::suspend_always{};\r\n}\r\n<\/pre>\n<p>The <code>suspend_<\/code><code>always<\/code> object suspends and never wakes up. Awaiting it will never complete. And that means that the <code>IAsyncAction<\/code> you created from it will never complete.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As much as anything is guaranteed to complete.<\/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-103610","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>As much as anything is guaranteed to complete.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103610","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=103610"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103610\/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=103610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=103610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=103610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}