Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Asynchronous programming in .NET

Asynchronous programming in .NET

Slides for the C# MeetUp in Barcelona

CPU-bound vs IO-bound, responsive UI in desktop applications, scalability and performance in web applications, obsolete patterns, Task.Run, Task.WhenAll, Task.WhenAny, CancellationToken, Timeout, Progress, performance test with Bombardier, best practices, etc.

Source code samples using .NET Core Preview 4 in https://github.com/gerardo-lijs/Asynchronous-Programming-Samples

Gerardo Lijs

April 30, 2019
Tweet

Other Decks in Programming

Transcript

  1. Content What is Asynchronous Programming CPU-bound vs IO-bound Obsolete patterns

    Tasks and async/await Practical use cases • Responsive user interfaces (WPF, WinForms, etc) • Scalability and performance in web applications @GerardoLijs
  2. CPU-bound operations • Use Task.Run to execute expensive CPU-bound code

    in the ThreadPool • Define a Task returning method • In general you will not need other methods of TaskFactory class @GerardoLijs Synchronous method Asynchronous method
  3. IO-bound operations • Hard drive • Network • Database •

    API calls • Hardware read/write (serial port, camera, sensor, etc) @GerardoLijs Unless you work in very specific industries, 95% or more of the code you write will never be CPU-bound. Most of the time you will need asynchronous to deal with IO-bound operations and the times you will use CPU-bound operations you will probably use a library that provides asynchronous methods for you to interact with.
  4. Responsive user interfaces @GerardoLijs Demo of a WPF desktop application

    • Targets .NET Core 3 Preview4 but works exactly the same with .NET 4.7.2 or earlier • The behaviour is the same in WinForms You will need https://dotnet.microsoft.com/download/dotnet-core/3.0 and VS 2019 enabled for .NET Core https://visualstudiomagazine.com/articles/2019/03/08/vs-2019-core-tip.aspx
  5. Responsive user interfaces • Use async with IO-bound operations whenever

    possible • Don’t block! • Use async for expensive CPU-bound operations • Use progress report and progress dialogs for better user experience • Allow user to cancel operations when possible • Use benchmarking tools such as BenchmarkDotNet (https://github.com/dotnet/BenchmarkDotNet) Summary @GerardoLijs Source code https://github.com/gerardo-lijs/Asynchronous-Programming-Samples
  6. Responsive user interfaces @GerardoLijs Demo of a Web API running

    IO-bound operation • Targets .NET Core 3 Preview4 but works exactly the same with .NET Core 2 • The behaviour is the same in Web API using .NET 4.7.2 or earlier Source code https://github.com/gerardo-lijs/Asynchronous-Programming-Samples
  7. Scalability and performance in web applications Simple API method that

    downloads a web page using synchronous code @GerardoLijs
  8. Scalability and performance in web applications Improves throughput because of

    reduced number of threads usage, less memory use and less CPU use Asynchronous benefits Source: https://caleblloyd.com/software/net-core-mvc-thread-pool-vs-async/ @GerardoLijs
  9. Responsive user interfaces @GerardoLijs Demo of a Web API running

    CPU-bound operation • Targets .NET Core 3 Preview4 but works exactly the same with .NET Core 2 • The behaviour is the same in Web API using .NET 4.7.2 or earlier Source code https://github.com/gerardo-lijs/Asynchronous-Programming-Samples
  10. Scalability and performance in web applications Synchronous CPU-bound Asynchronous CPU-bound

    Using async/await makes it worse in this case! @GerardoLijs
  11. Scalability and performance in web applications • Use async with

    IO-bound operations whenever possible • Don’t block! • Don’t use async for expensive CPU-bound operations • Use benchmarking tools such as Bombardier (https://github.com/codesenberg/bombardier) I still need more performance, what can I do? • Scale up • Scale out whole application using Docker/Kubernetes • Scale out only expensive CPU-bound operations to Azure Functions Summary @GerardoLijs
  12. Run a Task with a Timeout • Use Extension method

    Source: David Fowler -> AspNetCoreDiagnosticScenarios/Scenarios/Infrastructure/TaskExtensions.cs @GerardoLijs
  13. Running asynchronous method synchronously Use helper method borrowed from Microsoft

    Source: https://cpratt.co/async-tips-tricks/ Call like this Warning Try to avoid this! Most of the times (probably always) there are better ways and you don’t need to run async code synchronously @GerardoLijs
  14. Async class constructors They are not available by default in

    the framework Option 1 - Refactor with InitializeAsync method Call like this Previous class Refactored class @GerardoLijs
  15. Async class constructors They are not available by default in

    the framework Option 2 - Refactor with Factory Pattern Call like this Previous class Refactored class @GerardoLijs
  16. Parallel programming @GerardoLijs Demo of a WPF application using TPL

    and Task.WhenAll • Uses ReactiveUI and MahApps but they are not a requirement • Uses OpenCvSharp library as example of expensive operations • Targets .NET Core 3 Preview4 but works exactly the same with .NET Core 2 • The behaviour is the same in Web API using .NET 4.7.2 or earlier https://github.com/gerardo-lijs/Asynchronous-Programming-Samples Source code
  17. Slides, resources and source code Gerardo Lijs Software Developer At

    Microptic SL Email [email protected] Twitter @GerardoLijs https://github.com/gerardo-lijs/CSharpMeetup-Mar2019 Thank You! https://github.com/gerardo-lijs/Asynchronous-Programming https://github.com/gerardo-lijs/Asynchronous-Programming-Samples