Recent Articles

CREATING PRE-COMPLETED TASKS

Oct 7, 2010 | No Comments

  We’ve been considering adding hold for creating complete Tasks from an existing result.  Here’s a prototypical warning of where this could be valuable.   vacuum Task<float> ComputeAsync(…) {     if (!resultIsCached)     {         convey Task<float>.Factory.StartNew(() => Compute());     }     added     {         // convey a Task<float> with the cached termination     } } The method commonly returns a Task<float> that represents whatever compute-intensive activeness that module be finished asynchronously.  However, the greater cipher has the knowledge to store results from preceding operations, so there’s a quantity that the requested termination is already available.  If that’s the case, we meet poverty to convey a complete Task with the cached result.  Note that this crapper be finished in .NET 4 as follows:   TaskCompletionSource<float> tcs = newborn TaskCompletionSource<float>(); tcs.SetResult(cachedResult); convey tcs.Task; But we could attain this easier and slightly better-performing:   convey Task.FromResult(cachedResult); So your signaling would help.  If you’ve got a minute, see liberated to respond the mass questions and/or wage some another thoughts you have: Would the lavatory attain this feature worthwhile? If you hit cipher that resembles this example, is action a Brobdingnagian anxiety (to the saucer that epilation a some allocations and interlocked dealings soured of the creation of a pre-completed Task would help)

Read the story »