{"id":55893,"date":"2009-06-03T12:33:00","date_gmt":"2009-06-03T12:33:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/pfxteam\/2009\/06\/03\/mechanisms-for-creating-tasks\/"},"modified":"2009-06-03T12:33:00","modified_gmt":"2009-06-03T12:33:00","slug":"mechanisms-for-creating-tasks","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/mechanisms-for-creating-tasks\/","title":{"rendered":"Mechanisms for Creating Tasks"},"content":{"rendered":"<p>The core entity in the Task Parallel Library around which everything else revolves is System.Threading.Tasks.Task.&nbsp; The most common way of creating a Task will be through the StartNew method on the TaskFactory class, a default instance of which is exposed through a static property on Task, e.g.<\/p>\n<blockquote>\n<p>var t = Task.Factory.StartNew(() =&gt; <br>{ <br>&nbsp;&nbsp;&nbsp; &hellip; \/\/ body of the task goes here <br>});<\/p>\n<\/blockquote>\n<p>There are, however, other ways of creating tasks.&nbsp; For example, while using StartNew is the preferred mechanism to create a Task and schedule\/start it, we do support separating those two operations into two discrete actions, e.g.<\/p>\n<blockquote>\n<p>var t = new Task(() =&gt; <br>{ <br>&nbsp;&nbsp;&nbsp; &hellip; \/\/ body of the task goes here <br>}); <br>&hellip; \/\/ the task has been created but not scheduled <br>t.Start(); \/\/ now schedule it<\/p>\n<\/blockquote>\n<p>Moreover, StartNew isn&rsquo;t alone on TaskFactory; other methods like ContinueWhenAll, ContinueWhenAny, and FromAsync may all be used to create Task instances. Task also exposes a ContinueWith mechanism that can be used to create a Task that will be scheduled when the antecedent task (the Task on which ContinueWith is being called) completes.<\/p>\n<p>Finally, the <a href=\"https:\/\/blogs.msdn.com\/pfxteam\/archive\/2009\/06\/02\/9685804.aspx\">TaskCompletionSource&lt;TResult&gt;<\/a> type can be used to create a Task&lt;TResult&gt; completely controlled by the completion source instance, through its SetResult, SetException, and SetCanceled methods (and their TrySet* variants).<\/p>\n<p>Each of these different ways of creating a Task has different behaviors associated with it.&nbsp; The differences between them may not be obvious at first, but with a little thought, it should be clear why things behave the way they do.&nbsp; For example, calling Start on a Task created by StartNew is invalid (i.e. results in an exception)&hellip; you can&rsquo;t start an already started Task.&nbsp; In contrast, a Task created by a Task&rsquo;s constructor won&rsquo;t have been scheduled, so it&rsquo;s perfectly valid to call Start on it.&nbsp; Calling Start on a Task returned by a TaskCompletionSource&lt;TResult&gt; makes little sense, as there&rsquo;s nothing to &ldquo;start&rdquo;, so that&rsquo;s invalid.&nbsp; It&rsquo;s invalid to call Start on a continuation task (e.g. one created by ContinueWith, ContinueWhenAny, or ContinueWhenAll) because the work should only be scheduled when the antecedent(s) has completed.&nbsp; And it&rsquo;s invalid to call Start on a task created by FromAsync, because the work being done has already been initiated through a call to the beginMethod passed to FromAsync.<\/p>\n<p>These kinds of behavioral differences can be quite useful when building up abstractions on top of tasks.&nbsp; For example, let&rsquo;s say I want to implement a factory method for creating &ldquo;delayed&rdquo; tasks, ones that won&rsquo;t actually be scheduled until some user-supplied timeout has occurred.&nbsp; One way to write this would be as follows:<\/p>\n<blockquote>\n<p>public static Task StartNewDelayed(int millisecondsDelay, Action action) <br>{ <br>&nbsp;&nbsp;&nbsp; \/\/ Validate arguments <br>&nbsp;&nbsp;&nbsp; if (millisecondsDelay &lt; 0) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new ArgumentOutOfRangeException(&#8220;millisecondsDelay&#8221;); <br>&nbsp;&nbsp;&nbsp; if (action == null) throw new ArgumentNullException(&#8220;action&#8221;); <\/p>\n<p>&nbsp;&nbsp;&nbsp; \/\/ Create the task <br>&nbsp;&nbsp;&nbsp; var t = new Task(action); <br>&nbsp;&nbsp;&nbsp; \/\/ Start a timer that will trigger it <br>&nbsp;&nbsp;&nbsp; var timer = new Timer( <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _ =&gt; t.Start(), null, millisecondsDelay, Timeout.Infinite);&nbsp;<br>&nbsp;&nbsp;&nbsp; t.ContinueWith(_ =&gt; timer.Dispose());<br>&nbsp;&nbsp;&nbsp; return t; <br>}<\/p>\n<\/blockquote>\n<p>This implementation creates a new Task to run the provided action, but doesn&rsquo;t immediately start it.&nbsp; Instead, it creates a Timer with the user-supplied delay, and when the timer expires, the Timer&rsquo;s callback starts the task.&nbsp; Once the timer has been started, the Task is returned to the user.<\/p>\n<p>One problem with this implementation, which you might have guess based on earlier paragraphs, is that the Task returned to the user was created using the Task&rsquo;s constructor.&nbsp; This means it can be explicitly Start&rsquo;d.&nbsp; And that means the Task returned from StartNewDelayed could be started by the consumer prior to the Timer firing.&nbsp; That&rsquo;s bad for two reasons: one, it breaks expectations about the behavior of the Task and the associated delay, and two, a Task may only be started once.&nbsp; If the Task is explicitly started and then the timer&rsquo;s callback tries to Start the Task, kaboom: Start will throw an exception (since the Task was already started), the exception will go unhandled, and the app will come crumbling down.<\/p>\n<p>Given what we now know about behaviors associated with creating tasks, we can use a different mechanism for creating a task that doesn&rsquo;t allow the Task to be explicitly started.<\/p>\n<blockquote>\n<p>public static Task StartNewDelayed(int millisecondsDelay, Action action) <br>{ <br>&nbsp;&nbsp;&nbsp; \/\/ Validate arguments <br>&nbsp;&nbsp;&nbsp; if (millisecondsDelay &lt; 0) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new ArgumentOutOfRangeException(&#8220;millisecondsDelay&#8221;); <br>&nbsp;&nbsp;&nbsp; if (action == null) throw new ArgumentNullException(&#8220;action&#8221;); <\/p>\n<p>&nbsp;&nbsp;&nbsp; \/\/ Create a trigger used to start the task <br>&nbsp;&nbsp;&nbsp; var tcs = new TaskCompletionSource&lt;object&gt;(); <\/p>\n<p>&nbsp;&nbsp;&nbsp; \/\/ Start a timer that will trigger it <br>&nbsp;&nbsp;&nbsp; var timer = new Timer( <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _ =&gt; tcs.SetResult(null), null, millisecondsDelay, Timeout.Infinite); <\/p>\n<p>&nbsp;&nbsp;&nbsp; \/\/ Create and return a task that will be scheduled when the trigger fires. <br>&nbsp;&nbsp;&nbsp; return tcs.Task.ContinueWith(_ =&gt; <br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; timer.Dispose();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; action();<br>&nbsp;&nbsp;&nbsp; }); <br>}<\/p>\n<\/blockquote>\n<p>In this new implementation, I&rsquo;ve taken advantage of the fact that a continuation task can&rsquo;t be explicitly started and can be used to run arbitrary user code.&nbsp; The timer is used to resolve a TaskCompletionSource&lt;TResult&gt;, and a continuation off of that completion source is used to run the action.&nbsp; It&rsquo;s that continuation that&rsquo;s returned.<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The core entity in the Task Parallel Library around which everything else revolves is System.Threading.Tasks.Task.&nbsp; The most common way of creating a Task will be through the StartNew method on the TaskFactory class, a default instance of which is exposed through a static property on Task, e.g. var t = Task.Factory.StartNew(() =&gt; { &nbsp;&nbsp;&nbsp; &hellip; [&hellip;]<\/p>\n","protected":false},"author":360,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[7908],"tags":[7907,7911,7909,7912],"class_list":["post-55893","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pfxteam","tag-net-4","tag-code-samples","tag-parallel-extensions","tag-task-parallel-library"],"acf":[],"blog_post_summary":"<p>The core entity in the Task Parallel Library around which everything else revolves is System.Threading.Tasks.Task.&nbsp; The most common way of creating a Task will be through the StartNew method on the TaskFactory class, a default instance of which is exposed through a static property on Task, e.g. var t = Task.Factory.StartNew(() =&gt; { &nbsp;&nbsp;&nbsp; &hellip; [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/55893","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/360"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=55893"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/55893\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=55893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=55893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=55893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}