Download presentation
Presentation is loading. Please wait.
1
Staying Afloat in the .NET Async Ocean
Mike Huguet Senior Premier Field Engineer CRM and Developer Support
2
Mike Huguet Senior Premier Field Engineer CRM and Developer Support
3
Objective Feel comfortable in making a decision of which pattern and constructs to use
4
Agenda Threading Basics Async Programming Parallel Programming
5
Agenda Threading Basics Async Programming Parallel Programming
6
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
7
Example - Juggling
8
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.
9
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.
10
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.
11
Agenda Threading Basics Async Programming Parallel Programming
12
Asynchronous Programming Patterns
Legacy System.Threading Async Programming Model (APM) Task-based Async Pattern (TAP) ***Modern Event-Based Async Programming Model (EAP)
13
System.Threading - Thread
ThreadStart Notes .NET 1.0 Expensive Risky Can be complex *Use ThreadPool. QueueUserWorkItem $$$
14
System.Threading - ThreadPool
Notes Managed a pool Handles multi-CPU Difficult for continuation Single param restriction $$$
15
Demo - Threading
16
Async Programming Model (APM)
BeginXxx EndXxx IAsyncResult Notes .NET 2.0 Supports continuation Uses IO OS Threads Supports cancellation Expensive Can be complex continuation
17
Demo – APM
18
Event-Based Async Programming Model (EAP)
XxxAsync XxxCompleted XxxCancelled Notes .NET 2.0 Expensive Risky Can be REALLY complex BackgroundWorker WebClient
19
Demo - EAP
20
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
21
Demo – TAP
22
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
23
Demo – Async/Await
24
Agenda Threading Basics Async Programming Parallel Programming
25
Task Parallel Library (TPL)
Parallel.For Parallel.ForEach Parallel.Invoke Dataflow Blocks Notes .NET 4.0 Not always async Extra overhead
27
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
28
Demo – Parallel
29
Resources Parallel Processing and Concurrency in the .NET Framework - Managed Threading Best Practices - Managed Threading Basics - The Asynchronous Programming Models - Asynchronous Programming with Async and Await - Parallel Programming in the .NET Framework -
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.