Best Practices for Using async and await

362 views

Published on

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

Published in: Software
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
362
On SlideShare
0
From Embeds
0
Number of Embeds
159
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Best Practices for Using async and await

  1. 1. Dobre prakse pri uporabi async in await Damir Arh, Razum d.o.o. Microsoft MVP @DamirArh http://www.damirscorner.com
  2. 2. Agenda
  3. 3. Sinhrono : asinhrono O odzivnem uporabniškem vmesniku
  4. 4. private void OnSync(object sender, RoutedEventArgs e) { StatusText.Text = "Processing..."; Thread.Sleep(_sleepPeriod); StatusText.Text = String.Empty; } Sinhrono izvajanje
  5. 5. private async void OnAsync(object sender, RoutedEventArgs e) { StatusText.Text = "Processing..."; await Task.Delay(_sleepPeriod); StatusText.Text = String.Empty; } Asinhrono izvajanje
  6. 6. async void? Ne, hvala!
  7. 7. async void private async void OnGetData(object sender, RoutedEventArgs e) { try { DownloadMessages(); await Task.Delay(75); StatusText.Text = $"Messages received: {_messages.Count}"; } catch (Exception exception) { StatusText.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); StatusText.Text = $"Messages received: {_messages.Count}"; } catch (Exception exception) { StatusText.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); StatusText.Text = $"Messages received: {_messages.Count}"; } catch (Exception exception) { StatusText.Text = exception.Message; } } private async Task DownloadMessagesAsync() { _messages = await _repository.GetMessagesAsync(); }
  10. 10. Lažno predstavljanje Ni vse asinhrono, kar je async
  11. 11. Testna koda var tasks = new Task[_iterations]; var stopwatch = new Stopwatch(); stopwatch.Start(); for (int i = 0; i < _iterations; i++) { tasks[i] = AsyncService.CallAsync(i); } Task.WaitAll(tasks); stopwatch.Stop(); var duration = stopwatch.ElapsedMilliseconds;
  12. 12. Prava in lažna implementacija public async Task TrueAsync(int index) { Console.WriteLine($"Start {index}"); await Task.Delay(millis); Console.WriteLine($"End {index}"); } public async Task FakeAsync(int index) { return await Task.Run(() => { Console.WriteLine($"Start {index}"); Thread.Sleep(Millis); Console.WriteLine($"End {index}"); }); }
  13. 13. Pravo asinhrono izvajanje
  14. 14. Vrsta Lažno asinhrono izvajanje Zaloga niti
  15. 15. V smrtnem objemu Glavna nit je le ena
  16. 16. 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; }
  17. 17. 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; }
  18. 18. ConfigureAwait pomaga Ne naredite si medvedje usluge
  19. 19. SynchronizationContext
  20. 20. Ključni poudarki
  21. 21. Viri http://bit.ly/ch9async http://bit.ly/AsyncAwaitBasics http://bit.ly/NoAsyncThread http://bit.ly/SyncCtx
  22. 22. Izpolnite anketo! Vam je bilo predavanje všeč? Ste se naučili kaj novega? Vaše mnenje nam veliko pomeni! Da bo NT konferenca prihodnje leto še boljša, vas prosimo, da izpolnite anketo o zadovoljstvu, ki jo najdete v svojem NTK spletnem profilu.

×