
{"id":1600,"date":"2015-03-29T14:58:57","date_gmt":"2015-03-29T18:58:57","guid":{"rendered":"http:\/\/www.ikriv.com\/blog\/?p=1600"},"modified":"2015-03-29T14:58:57","modified_gmt":"2015-03-29T18:58:57","slug":"how-async-void-actually-works","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=1600","title":{"rendered":"How &quot;async void&quot; actually works"},"content":{"rendered":"<p>Let&#8217;s say I have this code (C#):<\/p>\n<p><code><\/p>\n<pre>void SomeFunc()\n{\n    HandlerOne();\n    HandlerTwo();\n}\n\nasync void HandlerOne()\n{\n    DoX();\n    await DoYAsync();\n    DoZ();\n}<\/pre>\n<p><\/code><\/p>\n<p>In this case HandlerOne() will return to the calling functions the moment it hits its first <code>await<\/code>. The code is roughly equivalent to:<\/p>\n<p><code><\/p>\n<pre>void HandlerOne()\n{\n    DoX();\n    Task t= DoYAsync();\n    t.ContinueWith(task=>DoZ());\n}<\/pre>\n<p><\/code><\/p>\n<p>So, there is no guarantee that <code>DoYAsync()<\/code> or <code>DoZ()<\/code> will be executed before <code>HandlerTwo()<\/code>. This is somewhat obvious after the fact (how else would one implement <code>async void<\/code>?),  but I did not see it explicitly described in any documentation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s say I have this code (C#): void SomeFunc() { HandlerOne(); HandlerTwo(); } async void HandlerOne() { DoX(); await DoYAsync(); DoZ(); } In this case HandlerOne() will return to the <a href=\"https:\/\/ikriv.com\/blog\/?p=1600\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[8],"tags":[],"class_list":["entry","author-ikriv","post-1600","post","type-post","status-publish","format-standard","category-cs"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1600","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1600"}],"version-history":[{"count":0,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1600\/revisions"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1600"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}