{"id":652,"date":"2016-11-23T22:25:20","date_gmt":"2016-11-23T22:25:20","guid":{"rendered":"https:\/\/ibex.tech\/windows-iot\/?p=172"},"modified":"2016-11-23T22:25:20","modified_gmt":"2016-11-23T22:25:20","slug":"async-await-task-etc","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/uwp-programming-in-c\/threads\/async-await-task-etc","title":{"rendered":"Async, Await, Task, etc"},"content":{"rendered":"\n<h5 class=\"wp-block-heading\">async<br><\/h5>\n\n\n\n<p>A Method declared with async (e.g. public async void MyFucntion(&#8230; ) &nbsp;will run asnychronously, which means execution of the Method calling it will continue before it is complete. &nbsp; <\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Task<br><\/h5>\n\n\n\n<p>If you want to force it to be a waiting to complete use &#8216;Task&#8217; as its return type&nbsp;(e.g. public async Task MyFucntion(&#8230; ) &nbsp; <\/p>\n\n\n\n<p>\n\tIf you don&#8217;t want to force a user to have to await,&nbsp;then don&#8217;t use &#8216;Task&#8217;.\n<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">await<br><\/h5>\n\n\n\n<p> To wait on an aync fucntion to complete when calling it use await (eg. MyVariable = await SomeMethod(&#8230; ) <\/p>\n\n\n\n<p>The catch &#8211; to use await the method calling it must be async! &nbsp; <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Asynchronous Gotchas in C#<\/h4>\n\n\n\n<p><a href=\"https:\/\/www.infoq.com\/news\/2013\/04\/async-csharp-fsharp\/\">https:\/\/www.infoq.com\/news\/2013\/04\/async-csharp-fsharp\/<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">How do I wait for an async method call to complete, in a method that isn&#8217;t async itself?<br><\/h4>\n\n\n\n<p>You typically don&#8217;t! &nbsp;Find a way round it either by not using await or by being able to make your method async itself. Its like this&nbsp;to try and force you to use threading properly, not to block the UI thread for instance, and to try and help you avoid lock up situations. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example method which uses await calls<br> <\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tprivate async void MyMethodName()\n\t{<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Allowing await to be used on the method or to return a value with await<br><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tprivate async Task MyMethodName()\n\t{<\/code><\/pre>\n\n\n\n<p>A value can be returned within the Task, so if you wanted to return an int: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tprivate async Task&lt;int&gt; MyMethodName()\n\t{<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>async A Method declared with async (e.g. public async void MyFucntion(&#8230; ) &nbsp;will run asnychronously, which means execution of the Method calling it will continue before it is complete. &nbsp; Task If you want to force it to be a waiting to complete use &#8216;Task&#8217; as its return type&nbsp;(e.g. public async Task MyFucntion(&#8230; ) &nbsp; [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69],"tags":[],"class_list":["post-652","post","type-post","status-publish","format-standard","hentry","category-threads"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/652","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=652"}],"version-history":[{"count":0,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/652\/revisions"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}