{"id":1843,"date":"2022-08-30T15:19:54","date_gmt":"2022-08-30T15:19:54","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/02\/android-asynctask-like-functionality-in-c-net-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:19:54","modified_gmt":"2022-08-30T15:19:54","slug":"android-asynctask-like-functionality-in-c-net-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/android-asynctask-like-functionality-in-c-net-collection-of-common-programming-errors\/","title":{"rendered":"Android AsyncTask-like functionality in C# (.NET)-Collection of common programming errors"},"content":{"rendered":"<p>As you might know, Android&#8217;s SDK features a <code>AsyncTask<\/code> class which allows for code execution on a separate thread and result acquisition in the main (UI) thread. In short, something like this:<\/p>\n<pre><code>class AsyncTask\n{\n   void onPreExecute(...)\n   {\n       \/\/Executed on the main thread BEFORE the secondary thread gets to work on anything.\n   }\n\n   void execute(...)\n   {\n       \/\/This runs on the secondary thread.\n   }\n\n   void onPostExecute(...)\n   {\n       \/\/This runs on the main thread AFTER the secondary thread finishes work. You get the result here in the form of some data type.\n   }\n}\n<\/code><\/pre>\n<p>Of course, this is just a rough scheme, but it should provide enough info on the AsyncTask if you&#8217;re not familiar with it. Basically, I&#8217;m looking for the same functionality in Microsoft&#8217;s .NET Framework.<\/p>\n<p>Before I start working on my own classes to do this, I want to be sure there&#8217;s nothing that could allow me to get the desired result already in the framework. I&#8217;m using .NET 4.0. Perhaps some sort of &#8216;clever&#8217; use of System.Threading.Tasks.Task? Have no idea, I&#8217;ll leave this to you.<\/p>\n<p>In short, I want to pass some input to a function, run the code on a secondary thread, and on completion, update some UI elements via the main thread. Locking the main thread (via <code>Task.Wait()<\/code> for example) doesn&#8217;t work well with my requirements.<\/p>\n<ol>\n<li>\n<p>You can achieve what you&#8217;re looking for using the <code>Task<\/code> class in .Net.<\/p>\n<p>Here&#8217;s what some code looks like that should help get you started:<\/p>\n<pre><code>var task = Task.Factory.StartNew(() =&gt; YourMethodGoesHere());\n\ntask.ContinueWith(t =&gt; UpdateYourUiInThisContinuation(),\n                  TaskScheduler.FromCurrentSynchronizationContext());\n\ntask.ContinueWith(t =&gt; HandleAnExceptionWhichTheTaskMayThrow(), \n                   TaskContinuationOptions.OnlyOnFaulted);\n<\/code><\/pre>\n<p>That will schedule the <code>YourMethodGoesHere()<\/code> to run off the UI thread. The continuation, <code>UpdateYourUiInThisContinuation()<\/code> will be scheduled to run once the initial task completes, and the overload I used will force it to continue within the same synchronization context (UI thread, assuming this code is being called on the UI thread initially).<\/p>\n<p>The last continuation is good practice to handle any exceptions which code within the task may thrown. If you don&#8217;t handle it (there are other ways other than using this continuation), you&#8217;ll end up with an unhandled AggregateException.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-12-02 20:57:42. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>As you might know, Android&#8217;s SDK features a AsyncTask class which allows for code execution on a separate thread and result acquisition in the main (UI) thread. In short, something like this: class AsyncTask { void onPreExecute(&#8230;) { \/\/Executed on the main thread BEFORE the secondary thread gets to work on anything. } void execute(&#8230;) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1843","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1843","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1843"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1843\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}