O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.
Dobre prakse pri
uporabi async in await
Damir Arh, Razum d.o.o.
Microsoft MVP
@DamirArh
http://www.damirscorner.com
Agenda
Sinhrono : asinhrono
O odzivnem uporabniškem vmesniku
private void OnSync(object sender, RoutedEventArgs e)
{
StatusText.Text = "Processing...";
Thread.Sleep(_sleepPeriod);
Sta...
private async void OnAsync(object sender, RoutedEventArgs e)
{
StatusText.Text = "Processing...";
await Task.Delay(_sleepP...
async void?
Ne, hvala!
async void
private async void OnGetData(object sender, RoutedEventArgs e)
{
try
{
DownloadMessages();
await Task.Delay(75)...
Lovljenje izjem
private async void OnGetData(object sender, RoutedEventArgs e)
{
try
{
DownloadMessages();
await Task.Dela...
Popravljena koda
private async void OnGetData(object sender, RoutedEventArgs e)
{
try
{
await DownloadMessagesAsync();
awa...
Lažno predstavljanje
Ni vse asinhrono, kar je async
Testna koda
var tasks = new Task[_iterations];
var stopwatch = new Stopwatch();
stopwatch.Start();
for (int i = 0; i < _it...
Prava in lažna implementacija
public async Task TrueAsync(int index)
{
Console.WriteLine($"Start {index}");
await Task.Del...
Pravo asinhrono izvajanje
Vrsta
Lažno asinhrono izvajanje
Zaloga niti
V smrtnem objemu
Glavna nit je le ena
Smrtni objem
private void OnDeadlock(object sender, RoutedEventArgs e)
{
var result = GetAsync().Result;
}
private async T...
Popravljena koda
private async void OnDeadlock(object sender, RoutedEventArgs e)
{
var result = await GetAsync().Result;
}...
ConfigureAwait pomaga
Ne naredite si medvedje usluge
SynchronizationContext
Ključni poudarki
Viri
http://bit.ly/ch9async
http://bit.ly/AsyncAwaitBasics
http://bit.ly/NoAsyncThread
http://bit.ly/SyncCtx
Izpolnite anketo!
Vam je bilo predavanje všeč?
Ste se naučili kaj novega?
Vaše mnenje nam veliko pomeni!
Da bo NT konferen...
Best Practices for Using async and await
Best Practices for Using async and await
Best Practices for Using async and await
Próximos SlideShares
Carregando em…5
×

Best Practices for Using async and await

544 visualizações

Publicada em

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

Publicada em: Software
  • Login to see the comments

  • Seja a primeira pessoa a gostar disto

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.

×