{"id":56068,"date":"2010-10-07T14:55:00","date_gmt":"2010-10-07T14:55:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/pfxteam\/2010\/10\/07\/creating-pre-completed-tasks\/"},"modified":"2010-10-07T14:55:00","modified_gmt":"2010-10-07T14:55:00","slug":"creating-pre-completed-tasks","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/creating-pre-completed-tasks\/","title":{"rendered":"Creating pre-completed Tasks"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p class=\"MsoNormal\">We&rsquo;ve been considering adding support for creating completed\nTasks from an existing result.&nbsp; Here&rsquo;s a\nprototypical example of where this could be valuable.<\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'courier new', courier\">void\nTask&lt;float&gt; ComputeAsync(&#8230;)<br>\n{<br>\n&nbsp;&nbsp;&nbsp; if (!resultIsCached)<br>\n&nbsp;&nbsp;&nbsp; {<br>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return\nTask&lt;float&gt;.Factory.StartNew(() =&gt; Compute());<br>\n&nbsp;&nbsp;&nbsp; }<br>\n&nbsp;&nbsp;&nbsp; else<br>\n&nbsp;&nbsp;&nbsp; {<br>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ return a Task&lt;float&gt; with\nthe cached result<br>\n&nbsp;&nbsp;&nbsp; }<br>\n}<\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'courier new', courier\"><br><\/span><\/p>\n<p class=\"MsoNormal\">The method usually returns a Task&lt;float&gt; that\nrepresents some compute-intensive operation that will be done asynchronously.&nbsp; However, the greater code has the ability to\ncache results from previous operations, so there&rsquo;s a chance that the requested\nresult is already available.&nbsp; If that&rsquo;s\nthe case, we just want to return a completed Task with the cached result.&nbsp; Note that this can be done in .NET 4 as\nfollows:<\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'courier new', courier\">TaskCompletionSource&lt;float&gt;\ntcs = new TaskCompletionSource&lt;float&gt;();<br>\ntcs.SetResult(cachedResult);<br>\nreturn tcs.Task;<\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'courier new', courier\"><br><\/span><\/p>\n<p class=\"MsoNormal\">But we could make this easier and slightly\nbetter-performing:<\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'courier new', courier\">return Task.FromResult(cachedResult);<\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'courier new', courier\"><br><\/span><\/p>\n<p class=\"MsoNormal\">So your input would help.&nbsp;\nIf you&rsquo;ve got a minute, feel free to answer the following questions\nand\/or provide any other thoughts you have:<\/p>\n<ol>\n<li>Would the convenience make this feature worthwhile?<\/li>\n<li>If you have code that resembles this example, is performance a huge concern (to the point that shaving a few allocations and interlocked operations off of the creation of a pre-completed Task would help)?<\/li>\n<li>What about support for creating other pre-completed Tasks, e.g. creating a pre-canceled Task or one from an existing Exception? &nbsp;Details regarding the scenario would help here.<\/li>\n<\/ol>\n<p class=\"MsoNormal\">Thanks!<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; We&rsquo;ve been considering adding support for creating completed Tasks from an existing result.&nbsp; Here&rsquo;s a prototypical example of where this could be valuable. &nbsp; void Task&lt;float&gt; ComputeAsync(&#8230;) { &nbsp;&nbsp;&nbsp; if (!resultIsCached) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return Task&lt;float&gt;.Factory.StartNew(() =&gt; Compute()); &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ return a Task&lt;float&gt; with the cached result &nbsp;&nbsp;&nbsp; [&hellip;]<\/p>\n","protected":false},"author":485,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[7908],"tags":[7907,7921,7927,7912],"class_list":["post-56068","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pfxteam","tag-net-4","tag-feedback-requested","tag-new-feature","tag-task-parallel-library"],"acf":[],"blog_post_summary":"<p>&nbsp; We&rsquo;ve been considering adding support for creating completed Tasks from an existing result.&nbsp; Here&rsquo;s a prototypical example of where this could be valuable. &nbsp; void Task&lt;float&gt; ComputeAsync(&#8230;) { &nbsp;&nbsp;&nbsp; if (!resultIsCached) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return Task&lt;float&gt;.Factory.StartNew(() =&gt; Compute()); &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ return a Task&lt;float&gt; with the cached result &nbsp;&nbsp;&nbsp; [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/56068","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\/485"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=56068"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/56068\/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=56068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=56068"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=56068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}