Make asynchronous method


/ Published in: C#
Save to your folder(s)

Make an existing calculation intensive method asynchronous.


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.