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.

Best Practices for Using async and await

293 views

Published on

The most common mistakes when using async await and how to avoid them

Published in: Software
  • Be the first to comment

  • Be the first to like this

Best Practices for Using async and await

  1. 1. Dobre prakse pri uporabi async in await Damir Arh, Razum d.o.o., Microsoft MVP
  2. 2. Agenda • Osnove • async void je nevaren • Lažne sinhrone metode • ConfigureAwait(false)
  3. 3. O odzivnem uporabniškem vmesniku Sinhrono : asinhrono
  4. 4. Sinhrono izvajanje private void OnSync(object sender, RoutedEventArgs e) { StatusText.Text = "Processing..."; Thread.Sleep(_sleepPeriod); StatusText.Text = String.Empty; }
  5. 5. Asinhrono izvajanje private async void OnAsync(object sender, RoutedEventArgs e) { StatusText.Text = "Processing..."; await Task.Delay(_sleepPeriod); StatusText.Text = String.Empty; }
  6. 6. Ne, hvala! async void?
  7. 7. async void private async void OnGetData(object sender, RoutedEventArgs e) { try { DownloadMessages(); await Task.Delay(75); Status.Text = $"Received: {_messages.Count}"; } catch (Exception exception) { Status.Text = exception.Message; } } private async void DownloadMessages() { _messages = await _repository.GetMessagesAsync(); }
  8. 8. Lovljenje izjem private async void OnGetData(object sender, RoutedEventArgs e) { try { DownloadMessages(); await Task.Delay(75); Status.Text = $"Received: {_messages.Count}"; } catch (Exception exception) { Status.Text = exception.Message; } } private async void DownloadMessages() { _messages = await _repository.GetMessagesAsync(); }
  9. 9. Popravljena koda private async void OnGetData(object sender, RoutedEventArgs e) { try { await DownloadMessagesAsync(); await Task.Delay(75); Status.Text = $"Received: {_messages.Count}"; } catch (Exception exception) { Status.Text = exception.Message; } } private async void Task DownloadMessagesAsync() { _messages = await _repository.GetMessagesAsync(); }
  10. 10. Glavna nit je le ena V smrtnem objemu
  11. 11. Smrtni objem private void OnDeadlock(object sender, RoutedEventArgs e) { var result = GetAsync().Result; } private async Task<string> GetAsync() { await Task.Delay(500); return String.Empty; }
  12. 12. Popravljena koda private async void OnDeadlock(object sender, RoutedEventArgs e) { var result = await GetAsync().Result; } private async Task<string> GetAsync() { await Task.Delay(500); return String.Empty; }
  13. 13. Ne naredite si medvedje usluge ConfigureAwait pomaga
  14. 14. SynchronizationContext • Abstrakcija privzetega izvajalnega konteksta • Odvisen od tipa aplikacije – WindowsForms – WPF – ASP.NET • Vračanje na privzeti kontekst – V aplikaciji običajno zaželeno – V knjižnicah tipično nepotrebno
  15. 15. Ključni poudarki • Uporabljajte async void le pri odzivih na dogodke • Uporabljajte asinhrone operacije kot takšne • Razmislite, kako se bodo asinhroni klici izvajali
  16. 16. DNC Magazine
  17. 17. Viri • http://bit.ly/ch9async • http://bit.ly/AsyncAwaitBasics • http://bit.ly/SyncCtx
  18. 18. @DamirArh http://www.damirscorner.com

×