Posted By

bokkers on 01/22/14


Tagged

Task Asynchronous async await


Versions (?)

Make asynchronous method


 / Published in: C#
 

Make an existing calculation intensive method asynchronous.

  1. public class ClassContainingCalculationIntensiveCode
  2. {
  3. public double SomeCalculation()
  4. {
  5. // code omitted
  6. }
  7.  
  8. // NOTE: Returning a new task object encapsulating the calculation is all that's needed.
  9. // The consuming code needs to use async/await keywords.
  10. public Task<double> SomeCalculationAsync()
  11. {
  12. return Task.Factory.StartNew(() =>
  13. {
  14. return SomeCalculation();
  15. });
  16. }
  17. }

Report this snippet  

You need to login to post a comment.