Best Practices for Using async and await

18
Dobre prakse pri uporabi async in await Damir Arh, Razum d.o.o., Microsoft MVP

Transcript of Best Practices for Using async and await

Page 1: Best Practices for Using async and await

Dobre prakse pri uporabiasync in await

Damir Arh, Razum d.o.o., Microsoft MVP

Page 2: Best Practices for Using async and await

Agenda

• Osnove• async void je nevaren• Lažne sinhrone metode• ConfigureAwait(false)

Page 3: Best Practices for Using async and await

O odzivnem uporabniškem vmesniku

Sinhrono : asinhrono

Page 4: Best Practices for Using async and await

Sinhrono izvajanje

private void OnSync(object sender, RoutedEventArgs e){ StatusText.Text = "Processing..."; Thread.Sleep(_sleepPeriod); StatusText.Text = String.Empty;}

Glavna nit

Page 5: Best Practices for Using async and await

Asinhrono izvajanje

private async void OnAsync(object sender, RoutedEventArgs e){ StatusText.Text = "Processing..."; await Task.Delay(_sleepPeriod); StatusText.Text = String.Empty;}

Glavna nit

Page 6: Best Practices for Using async and await

Ne, hvala!

async void?

Page 7: Best Practices for Using async and await

async voidprivate 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; }}

Glavna nit

private async void DownloadMessages(){ _messages = await _repository.GetMessagesAsync();}

Page 8: Best Practices for Using async and await

Lovljenje izjemprivate 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; }}

Glavna nit

private async void DownloadMessages(){ _messages = await _repository.GetMessagesAsync();}

Page 9: Best Practices for Using async and await

Popravljena kodaprivate 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; }}

Glavna nit

private async void Task DownloadMessagesAsync(){ _messages = await _repository.GetMessagesAsync();}

Page 10: Best Practices for Using async and await

Glavna nit je le ena

V smrtnem objemu

Page 11: Best Practices for Using async and await

Smrtni objem

private void OnDeadlock(object sender, RoutedEventArgs e){ var result = GetAsync().Result;}Glavna nit

private async Task<string> GetAsync(){ await Task.Delay(500); return String.Empty;}

Page 12: Best Practices for Using async and await

Popravljena koda

private async void OnDeadlock(object sender, RoutedEventArgs e){ var result = await GetAsync().Result;}Glavna nit

private async Task<string> GetAsync(){ await Task.Delay(500); return String.Empty;}

Page 13: Best Practices for Using async and await

Ne naredite si medvedje usluge

ConfigureAwait pomaga

Page 14: Best Practices for Using async and await

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

Page 15: Best Practices for Using async and await

Ključni poudarki

• Uporabljajte async void le pri odzivih na dogodke

• Uporabljajte asinhrone operacije kot takšne• Razmislite, kako se bodo asinhroni klici izvajali

Page 17: Best Practices for Using async and await

Viri

• http://bit.ly/ch9async • http://bit.ly/AsyncAwaitBasics• http://bit.ly/SyncCtx

Page 18: Best Practices for Using async and await

@DamirArhhttp://www.damirscorner.com