{"id":1830,"date":"2015-07-07T22:28:35","date_gmt":"2015-07-07T21:28:35","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=1830"},"modified":"2015-07-07T22:28:35","modified_gmt":"2015-07-07T21:28:35","slug":"await-and-async","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2015\/07\/07\/await-and-async\/","title":{"rendered":"Await and async"},"content":{"rendered":"<p>I was working on an project that was using the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/hh191443.aspx\" target=\"_blank\">async and await<\/a> keywords and an college asked what\/why use the await\/ async keywords when you will still be waiting on the task \/ thread to finish.  Which was an very good question, and the best answer that I could give (of which I am writing it here) is that if you want to start an thread and let it process it&#8217;s work whilst still carrying on with the main thread then you would need to write some thread controls etc.  But .Net gives you that thread control with the async \/ await keywords.<\/p>\n<p>The async is short for <a href=\"https:\/\/en.wikipedia.org\/wiki\/Asynchronous_I\/O\" target=\"_blank\">asynchronous<\/a> which basically means do something whilst doing something else, e.g. you are able to talk and listen at the same time!.<\/p>\n<p>So what happens is that you can create an method to do some long processing, I am just using an Task delay here to simulate that with also using the await syntax just before it so that it will actually wait for the task delay to finish, as below<\/p>\n<pre lang=\"csharp\">\r\n public async Task<Boolean> getCheckAsync(Boolean c)\r\n        {\r\n            System.Console.WriteLine(\"getCheckAsync about to wait\");\r\n            await Task.Delay(1000);\r\n            System.Console.WriteLine(\"getCheckAsync finished waiting\");\r\n            \/\/ do some processing.. pulling data from a database \/ website etc.\r\n            if (c)\r\n                return false;\r\n            else\r\n                return true;\r\n        }\r\n<\/pre>\n<p>You have to decorate the method call with async Task<return type>, since then you will be able to do some await on this method.<\/p>\n<p>The next part is to create an call to this method, and this will start the call as well &#8212; start processing the method of getCheckAsync<\/p>\n<pre lang=\"csharp\">\r\n Task<Boolean> getCheckAsyncTask = this.getCheckAsync(t);            \/\/ calls the process to start processing -- e.g. will output \"getCheckAsync about to wait\"\r\n<\/pre>\n<p>And to await for the result, which means that you are able to do some other actions in between the calling the method (getCheckAsync) and waiting on the result.  <\/p>\n<pre lang=\"csharp\">\r\nBoolean res = await getCheckAsyncTask;        \r\n<\/pre>\n<p>So from the full code example below, the first output would be <\/p>\n<pre lang=\"bash\">\r\nCalling the async\r\nWaiting for result from getCheckAsync\r\ngetCheckAsync about to wait\r\nBit inbetween the getCheckAsync call\r\n<\/pre>\n<p>which as you can see has already called the getCheckAsync method and then carried on processing the bit inbetween that is not reliant on the result of the method call (getCheckAsync), but once we are ready for the result of the getCheckAsync method call, the output would carry on with<\/p>\n<pre lang=\"bash\">\r\ngetCheckAsync finished waiting\r\nObtainted the result from getCheckAsync\r\nPassed the 'Calling the async'\r\nB = False\r\n<\/pre>\n<p>Here is the code in full<\/p>\n<pre lang=\"csharp\">\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.Text;\r\nusing System.Threading.Tasks;\r\n\r\nnamespace asynctest\r\n{\r\n    class Program\r\n    {\r\n        static void Main(string[] args)\r\n        {\r\n            Program p = new Program();\r\n\r\n            System.Console.WriteLine(\"Calling the async \");\r\n\r\n            Boolean b = p.getCheck(true).Result; \r\n\r\n            System.Console.WriteLine(\"Passed the 'Calling the async'\");\r\n            System.Console.WriteLine(\"B = \"+b);\r\n            System.Console.ReadLine();\r\n        }\r\n\r\n        public async Task<Boolean> getCheck(Boolean t)\r\n        {\r\n            System.Console.WriteLine(\"Waiting for result from getCheckAsync \");\r\n            Task<Boolean> getCheckAsyncTask = this.getCheckAsync(t);            \/\/ calls the process to start processing -- e.g. will output \"getCheckAsync about to wait\"\r\n            System.Console.WriteLine(\"Bit inbetween the getCheckAsync call\");   \/\/ do some extra processing.\r\n            Boolean res = await getCheckAsyncTask;                              \/\/ but now we have to \"await\" for the result.\r\n            System.Console.WriteLine(\"Obtainted the result from getCheckAsync \");\r\n            return res;\r\n        }\r\n\r\n        public async Task<Boolean> getCheckAsync(Boolean c)\r\n        {\r\n            System.Console.WriteLine(\"getCheckAsync about to wait\");\r\n            await Task.Delay(1000);\r\n            System.Console.WriteLine(\"getCheckAsync finished waiting\");\r\n            \/\/ do some processing.. pulling data from a database \/ website etc.\r\n            if (c)\r\n                return false;\r\n            else\r\n                return true;\r\n        }\r\n\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I was working on an project that was using the async and await keywords and an college asked what\/why use the await\/ async keywords when you will still be waiting on the task \/ thread to finish. Which was an very good question, and the best answer that I could give (of which I am &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2015\/07\/07\/await-and-async\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Await and async<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[396,395],"class_list":["post-1830","post","type-post","status-publish","format-standard","hentry","category-c_sharp","tag-async","tag-await"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1830","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=1830"}],"version-history":[{"count":2,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1830\/revisions"}],"predecessor-version":[{"id":1832,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1830\/revisions\/1832"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=1830"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=1830"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=1830"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}