Staying Afloat in the .NET Async Ocean Mike Huguet Senior Premier Field Engineer CRM and Developer Support
Mike Huguet Senior Premier Field Engineer CRM and Developer Support
Objective Feel comfortable in making a decision of which pattern and constructs to use
Agenda Threading Basics Async Programming Parallel Programming
Agenda Threading Basics Async Programming Parallel Programming
Asynchronous vs. Parallel Dealing with a lot of things at once Responsive User Interface Prevent blocking Can delegate to another A.k.a.-concurrency Parallel Doing a lot of things at once Requires asynchronous Break up work or tasks
Example - Juggling
What is a thread? Unit allocated to processor time by the OS PRISM FY16 Unit allocated to processor time by the OS Multiple threads can be allocated per process The OS and .NET create the effect of simultaneous thread execution 11/14/2018 2:53 PM What is a thread? First, let’s level set on what culture is. Culture is an outcome, it is the result of how we behave and work in our system. It is guided by decisions and what is reinforced. © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Threading disadvantage? PRISM FY16 Can strain OS resources and degrade performance Overhead exists for managing and using threads Cross thread communication costs Can require resource synchronization Adds complexity to code and is a high cause of bugs (e.g.-deadlocking, race conditions) 11/14/2018 2:53 PM Threading disadvantage? First, let’s level set on what culture is. Culture is an outcome, it is the result of how we behave and work in our system. It is guided by decisions and what is reinforced. © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Threading benefits? Effective way to increase responsiveness PRISM FY16 Effective way to increase responsiveness 11/14/2018 2:53 PM Threading benefits? First, let’s level set on what culture is. Culture is an outcome, it is the result of how we behave and work in our system. It is guided by decisions and what is reinforced. © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Agenda Threading Basics Async Programming Parallel Programming
Asynchronous Programming Patterns Legacy System.Threading Async Programming Model (APM) Task-based Async Pattern (TAP) ***Modern Event-Based Async Programming Model (EAP)
System.Threading - Thread ThreadStart Notes .NET 1.0 Expensive Risky Can be complex *Use ThreadPool. QueueUserWorkItem $$$
System.Threading - ThreadPool Notes Managed a pool Handles multi-CPU Difficult for continuation Single param restriction $$$
Demo - Threading
Async Programming Model (APM) BeginXxx EndXxx IAsyncResult Notes .NET 2.0 Supports continuation Uses IO OS Threads Supports cancellation Expensive Can be complex continuation
Demo – APM
Event-Based Async Programming Model (EAP) XxxAsync XxxCompleted XxxCancelled Notes .NET 2.0 Expensive Risky Can be REALLY complex BackgroundWorker WebClient
Demo - EAP
Task-based Asynchronous Pattern (TAP) XxxAsync Task.Run Task.Factory .StartNew Task<TResult> CancellationToken IProgress Notes .NET 4.0 Single method Supports multiple typed params Supports typed result Supports continuation & Cancel
Demo – TAP
TAP – Async/Await XxxAsync Task Task<TResult> Progress Notes .NET 4.5 Much easier continuation Compiler sugar Can be confusing Finally blocks might not run
Demo – Async/Await
Agenda Threading Basics Async Programming Parallel Programming
Task Parallel Library (TPL) Parallel.For Parallel.ForEach Parallel.Invoke Dataflow Blocks Notes .NET 4.0 Not always async Extra overhead
Parallel LINQ (PLINQ) ParallelQuery AsParallel Notes ParallelEnumerable ParallelQuery AsParallel Notes .NET 4.0 Familiar LINQ syntax Tries to optimize loading on multiple threads and cores Not necessarily faster
Demo – Parallel
Resources Parallel Processing and Concurrency in the .NET Framework - https://msdn.microsoft.com/en-us/library/hh156548(v=vs.110).aspx Managed Threading Best Practices - https://msdn.microsoft.com/en-us/library/1c9txz50(v=vs.110).aspx Managed Threading Basics - https://msdn.microsoft.com/en-us/library/hyz69czz(v=vs.110).aspx The Asynchronous Programming Models - http://blogs.msdn.com/b/mazhou/archive/2011/10/04/the-asynchronous-programming-models.aspx Asynchronous Programming with Async and Await - https://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx Parallel Programming in the .NET Framework - https://msdn.microsoft.com/en-us/library/dd460693(v=vs.110).aspx