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.

Correcting Common .NET Async/Await Mistakes

476 views

Published on

Did you know that the .NET compiler turns our async methods into classes? And that .NET adds a try/catch block to each of these classes, potentially hiding thrown exceptions? It's true! In this session, we will learn how to best use async/await in C# by analyzing how .NET compiles our async code. Join me as we take an existing app and optimize its async code together, showing off performance gains, better exception handling, improved run-time speed, and smaller app size!

Published in: Technology
  • Be the first to comment

Correcting Common .NET Async/Await Mistakes

  1. 1. @TheCodeTraveler https://codetraveler.io/Techorama-AsyncAwait
  2. 2. @TheCodeTraveler https://codetraveler.io/Techorama-AsyncAwait async Task ReadDataFromUrl(string url) { WebClient wc = new WebClient(); byte[] result = await wc.DownloadDataTaskAsync(url); string data = Encoding.ASCII.GetString(result); LoadData(data); }
  3. 3. @TheCodeTraveler https://codetraveler.io/Techorama-AsyncAwait async Task ReadDataFromUrl(string url) { WebClient wc = new WebClient(); byte[] result = await wc.DownloadDataTaskAsync(url); string data = Encoding.ASCII.GetString(result); LoadData(data); } Thread 1
  4. 4. @TheCodeTraveler https://codetraveler.io/Techorama-AsyncAwait async Task ReadDataFromUrl(string url) { WebClient wc = new WebClient(); byte[] result = await wc.DownloadDataTaskAsync(url); string data = Encoding.ASCII.GetString(result); LoadData(data); } Thread 2* *Can be any thread other than Thread 1 e.g. Thread 32
  5. 5. @TheCodeTraveler https://codetraveler.io/Techorama-AsyncAwait async Task ReadDataFromUrl(string url) { WebClient wc = new WebClient(); byte[] result = await wc.DownloadDataTaskAsync(url); string data = Encoding.ASCII.GetString(result); LoadData(data); } Thread 1
  6. 6. @TheCodeTraveler https://codetraveler.io/Techorama-AsyncAwait async Task ReadDataFromUrl(string url) { WebClient wc = new WebClient(); byte[] result = await wc.DownloadDataTaskAsync(url); string data = Encoding.ASCII.GetString(result); LoadData(data); }
  7. 7. @TheCodeTraveler https://codetraveler.io/Techorama-AsyncAwait async Task ReadDataFromUrl(string url) { WebClient wc = new WebClient(); byte[] result = await wc.DownloadDataTaskAsync(url); string data = Encoding.ASCII.GetString(result); LoadData(data); } private sealed class <ReadDataFromUrl>d_1 : IAsyncStateMachine
  8. 8. @TheCodeTraveler https://codetraveler.io/Techorama-AsyncAwait async Task ReadDataFromUrl(string url) { WebClient wc = new WebClient(); byte[] result = await wc.DownloadDataTaskAsync(url); string data = Encoding.ASCII.GetString(result); LoadData(data); } private string <data>5_3; private byte[] <result>5_2; private WebClient <wc>5_1; public string url;
  9. 9. @TheCodeTraveler https://codetraveler.io/Techorama-AsyncAwait async Task ReadDataFromUrl(string url) { WebClient wc = new WebClient(); byte[] result = await wc.DownloadDataTaskAsync(url); string data = Encoding.ASCII.GetString(result); LoadData(data); } private void MoveNext();
  10. 10. @TheCodeTraveler https://codetraveler.io/Techorama-AsyncAwait public void MoveNext() { uint num = (uint)this.$PC; this.$PC = -1; try { switch (num) { case 0: this.<wc>__0 = new WebClient(); this.$awaiter0 = this.<wc>__0.DownloadDataTaskAsync(this.url).GetAwaiter(); this.$PC = 1; ... return; break; case 1: this.<result>__1 = this.$awaiter0.GetResult(); this.<data>__2 = Encoding.ASCII.GetString(this.<result>__1); this.$this.LoadData(this.<data>__2); break; default: return; } } catch (Exception exception) { ... } this.$PC = -1; this.$builder.SetResult(); }
  11. 11. @TheCodeTraveler https://codetraveler.io/Techorama-AsyncAwait public void MoveNext() { uint num = (uint)this.$PC; this.$PC = -1; try { switch (num) { case 0: this.<wc>__0 = new WebClient(); this.$awaiter0 = this.<wc>__0.DownloadDataTaskAsync(this.url).GetAwaiter(); this.$PC = 1; ... return; break; case 1: this.<result>__1 = this.$awaiter0.GetResult(); this.<data>__2 = Encoding.ASCII.GetString(this.<result>__1); this.$this.LoadData(this.<data>__2); break; default: return; } } catch (Exception exception) { ... } this.$PC = -1; this.$builder.SetResult(); } case 0: this.<wc>__0 = new WebClient(); this.$awaiter0 = this.<wc>__0.DownloadDataTaskAsync(this.url).GetAwaiter(); this.$PC = 1; ... return;
  12. 12. @TheCodeTraveler https://codetraveler.io/Techorama-AsyncAwait public void MoveNext() { uint num = (uint)this.$PC; this.$PC = -1; try { switch (num) { case 0: this.<wc>__0 = new WebClient(); this.$awaiter0 = this.<wc>__0.DownloadDataTaskAsync(this.url).GetAwaiter(); this.$PC = 1; ... return; break; case 1: this.<result>__1 = this.$awaiter0.GetResult(); this.<data>__2 = Encoding.ASCII.GetString(this.<result>__1); this.$this.LoadData(this.<data>__2); break; default: return; } } catch (Exception exception) { ... } this.$PC = -1; this.$builder.SetResult(); } case 1: this.<result>__1 = this.$awaiter0.GetResult(); this.<data>__2 = Encoding.ASCII.GetString(this.<result>__1); this.$this.LoadData(this.<data>__2); break;
  13. 13. @TheCodeTraveler https://codetraveler.io/Techorama-AsyncAwait public void MoveNext() { uint num = (uint)this.$PC; this.$PC = -1; try { switch (num) { case 0: this.<wc>__0 = new WebClient(); this.$awaiter0 = this.<wc>__0.DownloadDataTaskAsync(this.url).GetAwaiter(); this.$PC = 1; ... return; break; case 1: this.<result>__1 = this.$awaiter0.GetResult(); this.<data>__2 = Encoding.ASCII.GetString(this.<result>__1); this.$this.LoadData(this.<data>__2); break; default: return; } } catch (Exception exception) { ... } this.$PC = -1; this.$builder.SetResult(); } try { catch (Exception exception) { . . . }
  14. 14. @TheCodeTraveler https://codetraveler.io/Techorama-AsyncAwait
  15. 15. @TheCodeTraveler https://codetraveler.io/Techorama-AsyncAwait
  16. 16. @TheCodeTraveler https://codetraveler.io/Techorama-AsyncAwait
  17. 17. @TheCodeTraveler https://codetraveler.io/Techorama-AsyncAwait
  18. 18. @TheCodeTraveler https://codetraveler.io/Techorama-AsyncAwait
  19. 19. @TheCodeTraveler https://codetraveler.io/Techorama-AsyncAwait
  20. 20. @TheCodeTraveler https://codetraveler.io/Techorama-AsyncAwait
  21. 21. @TheCodeTraveler https://codetraveler.io/Techorama-AsyncAwait
  22. 22. @TheCodeTraveler https://codetraveler.io/Techorama-AsyncAwait
  23. 23. @TheCodeTraveler https://codetraveler.io/Techorama-AsyncAwait

×