Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

await Xamarin @ PTXug

755 views

Published on

C# aync-await best practices

Published in: Technology
  • Be the first to comment

await Xamarin @ PTXug

  1. 1. Lisbon, May 30th 2015 await Xamarin() PTXug - Xamarin Talks #1
  2. 2. Speaker http://PauloMorgado.NET/ http://about.me/PauloMorgado http://www.slideshare.net/PauloJorgeMorgado http://pontonetpt.org/blogs/paulomorgado/ http://blogs.msmvps.com/paulomorgado/ http://weblogs.asp.net/paulomorgado http://www.revista-programar.info/author/pmorgado/ C:> ping me @paulomorgado
  3. 3. Agenda
  4. 4. For goodness’ sake, stop using async void!
  5. 5. async void is only for event handlers
  6. 6. Events are not going away.
  7. 7. async over events
  8. 8. Is it CPU-bound, or I/O-bound?
  9. 9. Thread pool
  10. 10. Don’t lie
  11. 11. Two ways of thinking about asynchrony Foo(); var task = FooAsync(); From the method signature (how people call it) void Foo() { for (int i=0; i<100; i++) Math.Sin(i); } From the method implementation (what resources it uses) async Task FooAsync() { await client.DownloadAsync(); }
  12. 12. Async methods: Your caller’s assumptions Is this true for your async methods?
  13. 13. Libraries shouldn’t use Task.Run()
  14. 14. Your callers should be the ones to call Task.Run synchronous block the current thread asynchronous without spawning new threads
  15. 15. Sync methods: Your caller’s assumptions void Foo() { FooAsync().Wait(); } -- will deadlock!!!
  16. 16. Task.Run() is the way to create new tasks
  17. 17. Task creation
  18. 18. await all the way
  19. 19. await all the way
  20. 20. Use ConfigureAwait(false)
  21. 21. SynchronizationContext
  22. 22. SynchronizationContext and await
  23. 23. SynchronizationContext: ConfigureAwait
  24. 24. Use the CancellationToken
  25. 25. Use the CancellationToken
  26. 26. Cache the returned Task<T>
  27. 27. Library perf considerations
  28. 28. Q & A
  29. 29. References http://blogs.msdn.com/b/lucian/archive/2013/11/23/talk-mvp-summit-async-best-practices.aspx http://channel9.msdn.com/Series/Three-Essential-Tips-for-Async/Three-Essential-Tips-For-Async- Introduction http://curah.microsoft.com/45553/asyncawait-general http://curah.microsoft.com/44400/async-and-aspnet https://github.com/paulomorgado/xamarin-xamarin-store-app
  30. 30. Sponsors
  31. 31. C:> ping me @paulomorgado

×