SlideShare a Scribd company logo
Presentation By:
Live App team.
Task Parallel Library(TPL)
Agenda
• Introduction
• Life Before Async/Await
• Synchronization context
• The Lifecycle of an Async Operation
• Misconception about async/await
• Difference between CPU bound work and I/O work
• Handling Exceptions
• Report Progress/Cancellation of a task
• Unit Testing
• Combinators
• Tips and tricks
Introduction
• What is Thread?
In computer science, a thread of execution is the smallest unit of processing that can be
scheduled by an operating system.
Introduction(Cont.)
• Blocking Code VS. Non Blocking code.(Demo)
Life Before Async/Await
• example is the method on DNS that looks up the
IP address for a hostname
Asynchronous Programming Model (APM)
Life Before Async/Await(Cont.)
• Event-based Asynchronous Pattern (EAP)
Example Downloading webpage and display it.
Life Before Async/Await (Cont.)
• Why these Patterns are messy?
Synchronization context
• It represents a target for work
• It’s been in the framework for a while, but we generally haven’t had to worry about it.
• For example, in Winforms, if you get the current SynchronizationContext and do Post on it, it
does a Control.BeginInvoke. That's how Winforms gets onto the UI thread.
• And ASP.Net current synchronization context, when you do Post() on it, it schedules work to be
done in its own way.
• There are about 10 in the framework, and you can create more.
• And this is the key way that the await keyword knows how to put you back where you were.
• So when you do await, it first captures the current SyncContext before awaiting.
• When it resumes, it uses SyncContext.Post() to resume "in the same place" as it was before.
The Lifecycle of an Async Operation
Misconception about async/await
• In each async method you write, some code will be before the first occurrence of the await
keyword. Equally, some code is in the expression that gets awaited. This code always runs in
the calling thread. Nothing interesting happens before the first await.
• This is one of the most common misconceptions about async. Async never schedules your
method to run on a background thread. The only way to do that is using something like
Task.Run, which is explicitly for that purpose.
Difference between CPU bound work and I/O work
CPU bound work
* CPU-bound means things like LINQ-to-objects, or iterations, or computationally-intensive inner
loops.
*Parallel.ForEach and Task.Run are good ways to put these CPU-bound workloads on the
threadpool.
*Use of threads will never increase throughput on a machine that’s under load.
I/O bound work
*Async methods are intended to be non-blocking operations. An await expression in an async method
doesn’t block the current thread while the awaited task is running. Instead, the expression signs up the rest
of the method as a continuation and returns control to the caller of the async method.(Ex. Database
requests, network access requests).
Exceptions in Async Code
Exceptions in Async Task-Returning Methods
async Task Catcher()
{
try
{
await Thrower();
}
catch (Exception)
{
// Execution will reach here
}
}
Exceptions in Async void Methods
Exceptions that leave an async void method are rethrown in the calling thread:
• If there was a SynchronizationContext when the async method was called, the exception is
Posted to it.
• If not, it is thrown on the thread pool.
Exceptions in Async void Methods (cont.)
Handle Exceptions in Async void
• In case of Console and Windows Forms application subscribe to
“AppDomain.CurrentDomain.UnhandledException” event.
• In case of Windows Store App subscribe to “Application.UnhandledException” event
AggregateException and WhenAll
List<Task> tasks = new List<Task> { Thrower1(), Thrower2()};
Task result = Task.WhenAll(tasks);
try
{
await result ;
}
catch (Exception)
{
foreach (Exception ex in result.Exception.InnerExceptions)
{}
}
Combinators
1. Task.WhenAll (params Task[] tasks)
• creates a task that will complete when all of the supplied tasks have completed. It will not block
the current execution but allows you to await the tasks for completion.
2. Task.WaitAll(params Task[] tasks)
• will wait for all of the provided Task objects to complete execution.This will block the current
execution until all tasks are done.
3. Task.WhenAny
4. Task.WaitAny :
• So if you are inside an async method, you probably want to use Task.WhenAll or WhenAny and
use await to get the results.
Tips n Tricks : Dispatcher
• On the Windows Platforms there is a rule that you cannot modify UI elements from secondary
threads. On Microsoft's XAML based platforms there is a member that exists on most UI
objects called the Dispatcher that can be used to marshal a call to the proper thread.
• Demo
• The CoreDispatcherPriority enumeration has these members.
Tips n Tricks : TaskCompletionSource
• Is a class which wraps a Task whose state
we can manually control. This is more
easily understood with an example
• It Mostly use it when only a event base
API is available
• Demo
Tips n Tricks : Configure await
• “Await task” uses the sync context
• 1. It captures the current SyncContext before awaiting.
• 2. Upon task completion, it calls SyncContext.Post() to resume “where you were before”
• You can use “await task.ConfigureAwait(false)”
• This suppresses step 2; instead if possible it resumes “on the thread that completed the task”
• Demo
Tips n Tricks : DeadLock
• Async All the Way
You should avoid mixing async and blocking code. Mixed async and blocking code can cause deadlocks,
more-complex error handling and unexpected blocking of context threads. The exception to this
guideline is the Main method for console applications, or—if you’re an advanced user—managing a
partially asynchronous codebase. Figure 5 The “Async Way” of Doing Things
• Demo
To Do This … Instead of This … Use This
Retrieve the result of a background task Task.Wait or Task.Result await
Wait for any task to complete Task.WaitAny await Task.WhenAny
Retrieve the results of multiple tasks Task.WaitAll await Task.WhenAll
Wait a period of time Thread.Sleep await Task.Delay

More Related Content

PPTX
LinkedIn 101: Create a Profile and Learn the Basics!
PPT
Salesforce Integration
PDF
10 steps to your career makeover
PDF
Angularjs - Unit testing introduction
PDF
REST API Basics
PDF
Secure Salesforce: External App Integrations
PDF
Startup Metrics 101: a Product & Marketing Workshop
PPTX
Learn SoapUI
LinkedIn 101: Create a Profile and Learn the Basics!
Salesforce Integration
10 steps to your career makeover
Angularjs - Unit testing introduction
REST API Basics
Secure Salesforce: External App Integrations
Startup Metrics 101: a Product & Marketing Workshop
Learn SoapUI

What's hot (20)

PPTX
Hexagonal architecture with Spring Boot
PPTX
Negative Testing
PPT
Oracle Fusion HCM Presentation
PDF
Introduction to SIP(Session Initiation Protocol)
PDF
Getting Started With Cypress
PPTX
Bain Resume Sample
PPTX
Build your QA Pipeline using Serenity , Selenium WebDriver , Rest Assured and...
ODP
Introduction to Spring Framework and Spring IoC
PDF
Introduction à l’intégration continue avec Jenkins
PDF
OOW15 - personalize and extend oracle ebs for desktops and tablets
PDF
Inverting The Testing Pyramid
DOCX
Operation Level Costing - Part 1
DOCX
Spring annotations notes
PDF
Karate - powerful and simple framework for REST API automation testing
PPTX
Async Programming in C# 5
PDF
Oracle Process Manufacturing for Specialty Chemicals - Value Proposition
DOCX
Henna Cv main
PPSX
PDF
12 Steps to API Load Testing with Apache JMeter
PPTX
Web Services and Introduction of SOAPUI
Hexagonal architecture with Spring Boot
Negative Testing
Oracle Fusion HCM Presentation
Introduction to SIP(Session Initiation Protocol)
Getting Started With Cypress
Bain Resume Sample
Build your QA Pipeline using Serenity , Selenium WebDriver , Rest Assured and...
Introduction to Spring Framework and Spring IoC
Introduction à l’intégration continue avec Jenkins
OOW15 - personalize and extend oracle ebs for desktops and tablets
Inverting The Testing Pyramid
Operation Level Costing - Part 1
Spring annotations notes
Karate - powerful and simple framework for REST API automation testing
Async Programming in C# 5
Oracle Process Manufacturing for Specialty Chemicals - Value Proposition
Henna Cv main
12 Steps to API Load Testing with Apache JMeter
Web Services and Introduction of SOAPUI
Ad

Similar to Task parallel library presentation (20)

PPSX
Async-await best practices in 10 minutes
PPTX
End to-end async and await
PDF
.NET Fest 2018. Владимир Крамар. Многопоточное и асинхронное программирование...
PPTX
C# Async/Await Explained
PPTX
History of asynchronous in .NET
PPTX
.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund
PPTX
Training – Going Async
PPTX
NDC Sydney 2019 - Async Demystified -- Karel Zikmund
PPTX
Async await
PPTX
The 3 VS Threading Rules
PPTX
Windows Phone 8 - 3.5 Async Programming
PPTX
Async/Await
PPTX
Async CTP 3 Presentation for MUGH 2012
PPTX
Async Programming with C#5: Basics and Pitfalls
PDF
Async await...oh wait!
PDF
Async Await for Mobile Apps
PDF
Async Debugging - A Practical Guide to survive !
PPTX
Concurrency - responsiveness in .NET
PPTX
Asynchronous programming - .NET Way
PDF
.NET Multithreading and File I/O
Async-await best practices in 10 minutes
End to-end async and await
.NET Fest 2018. Владимир Крамар. Многопоточное и асинхронное программирование...
C# Async/Await Explained
History of asynchronous in .NET
.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund
Training – Going Async
NDC Sydney 2019 - Async Demystified -- Karel Zikmund
Async await
The 3 VS Threading Rules
Windows Phone 8 - 3.5 Async Programming
Async/Await
Async CTP 3 Presentation for MUGH 2012
Async Programming with C#5: Basics and Pitfalls
Async await...oh wait!
Async Await for Mobile Apps
Async Debugging - A Practical Guide to survive !
Concurrency - responsiveness in .NET
Asynchronous programming - .NET Way
.NET Multithreading and File I/O
Ad

Recently uploaded (20)

PDF
Modernizing your data center with Dell and AMD
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
KodekX | Application Modernization Development
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Encapsulation theory and applications.pdf
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
cuic standard and advanced reporting.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Modernizing your data center with Dell and AMD
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
KodekX | Application Modernization Development
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Encapsulation theory and applications.pdf
NewMind AI Monthly Chronicles - July 2025
Advanced methodologies resolving dimensionality complications for autism neur...
Encapsulation_ Review paper, used for researhc scholars
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Digital-Transformation-Roadmap-for-Companies.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
MYSQL Presentation for SQL database connectivity
cuic standard and advanced reporting.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025

Task parallel library presentation

  • 1. Presentation By: Live App team. Task Parallel Library(TPL)
  • 2. Agenda • Introduction • Life Before Async/Await • Synchronization context • The Lifecycle of an Async Operation • Misconception about async/await • Difference between CPU bound work and I/O work • Handling Exceptions • Report Progress/Cancellation of a task • Unit Testing • Combinators • Tips and tricks
  • 3. Introduction • What is Thread? In computer science, a thread of execution is the smallest unit of processing that can be scheduled by an operating system.
  • 4. Introduction(Cont.) • Blocking Code VS. Non Blocking code.(Demo)
  • 5. Life Before Async/Await • example is the method on DNS that looks up the IP address for a hostname Asynchronous Programming Model (APM)
  • 6. Life Before Async/Await(Cont.) • Event-based Asynchronous Pattern (EAP) Example Downloading webpage and display it.
  • 7. Life Before Async/Await (Cont.) • Why these Patterns are messy?
  • 8. Synchronization context • It represents a target for work • It’s been in the framework for a while, but we generally haven’t had to worry about it. • For example, in Winforms, if you get the current SynchronizationContext and do Post on it, it does a Control.BeginInvoke. That's how Winforms gets onto the UI thread. • And ASP.Net current synchronization context, when you do Post() on it, it schedules work to be done in its own way. • There are about 10 in the framework, and you can create more. • And this is the key way that the await keyword knows how to put you back where you were. • So when you do await, it first captures the current SyncContext before awaiting. • When it resumes, it uses SyncContext.Post() to resume "in the same place" as it was before.
  • 9. The Lifecycle of an Async Operation
  • 10. Misconception about async/await • In each async method you write, some code will be before the first occurrence of the await keyword. Equally, some code is in the expression that gets awaited. This code always runs in the calling thread. Nothing interesting happens before the first await. • This is one of the most common misconceptions about async. Async never schedules your method to run on a background thread. The only way to do that is using something like Task.Run, which is explicitly for that purpose.
  • 11. Difference between CPU bound work and I/O work CPU bound work * CPU-bound means things like LINQ-to-objects, or iterations, or computationally-intensive inner loops. *Parallel.ForEach and Task.Run are good ways to put these CPU-bound workloads on the threadpool. *Use of threads will never increase throughput on a machine that’s under load. I/O bound work *Async methods are intended to be non-blocking operations. An await expression in an async method doesn’t block the current thread while the awaited task is running. Instead, the expression signs up the rest of the method as a continuation and returns control to the caller of the async method.(Ex. Database requests, network access requests).
  • 12. Exceptions in Async Code Exceptions in Async Task-Returning Methods async Task Catcher() { try { await Thrower(); } catch (Exception) { // Execution will reach here } }
  • 13. Exceptions in Async void Methods Exceptions that leave an async void method are rethrown in the calling thread: • If there was a SynchronizationContext when the async method was called, the exception is Posted to it. • If not, it is thrown on the thread pool.
  • 14. Exceptions in Async void Methods (cont.) Handle Exceptions in Async void • In case of Console and Windows Forms application subscribe to “AppDomain.CurrentDomain.UnhandledException” event. • In case of Windows Store App subscribe to “Application.UnhandledException” event
  • 15. AggregateException and WhenAll List<Task> tasks = new List<Task> { Thrower1(), Thrower2()}; Task result = Task.WhenAll(tasks); try { await result ; } catch (Exception) { foreach (Exception ex in result.Exception.InnerExceptions) {} }
  • 16. Combinators 1. Task.WhenAll (params Task[] tasks) • creates a task that will complete when all of the supplied tasks have completed. It will not block the current execution but allows you to await the tasks for completion. 2. Task.WaitAll(params Task[] tasks) • will wait for all of the provided Task objects to complete execution.This will block the current execution until all tasks are done. 3. Task.WhenAny 4. Task.WaitAny : • So if you are inside an async method, you probably want to use Task.WhenAll or WhenAny and use await to get the results.
  • 17. Tips n Tricks : Dispatcher • On the Windows Platforms there is a rule that you cannot modify UI elements from secondary threads. On Microsoft's XAML based platforms there is a member that exists on most UI objects called the Dispatcher that can be used to marshal a call to the proper thread. • Demo • The CoreDispatcherPriority enumeration has these members.
  • 18. Tips n Tricks : TaskCompletionSource • Is a class which wraps a Task whose state we can manually control. This is more easily understood with an example • It Mostly use it when only a event base API is available • Demo
  • 19. Tips n Tricks : Configure await • “Await task” uses the sync context • 1. It captures the current SyncContext before awaiting. • 2. Upon task completion, it calls SyncContext.Post() to resume “where you were before” • You can use “await task.ConfigureAwait(false)” • This suppresses step 2; instead if possible it resumes “on the thread that completed the task” • Demo
  • 20. Tips n Tricks : DeadLock • Async All the Way You should avoid mixing async and blocking code. Mixed async and blocking code can cause deadlocks, more-complex error handling and unexpected blocking of context threads. The exception to this guideline is the Main method for console applications, or—if you’re an advanced user—managing a partially asynchronous codebase. Figure 5 The “Async Way” of Doing Things • Demo To Do This … Instead of This … Use This Retrieve the result of a background task Task.Wait or Task.Result await Wait for any task to complete Task.WaitAny await Task.WhenAny Retrieve the results of multiple tasks Task.WaitAll await Task.WhenAll Wait a period of time Thread.Sleep await Task.Delay

Editor's Notes

  • #4: Notes -A thread is a single sequential flow of control within a program. -A thread in computer science is short for a thread of execution. Threads are a way for a program to divide (termed "split") itself into two or more simultaneously (or pseudo-simultaneously) running tasks. Threads and processes differ from one operating system to another but, in general, a thread is contained inside a process and different threads in the same process share same resources while different processes in the same multitasking operating system do not.
  • #8: To Do: Add table comparison.
  • #10: 1-The user clicks the button, so the event handler GetButton_OnClick is queued. 2. The UI thread executes the first half of GetButton_OnClick, including the call to GetFaviconAsync. 3. The UI thread continues into GetFaviconAsync and executes the first half of it, including the call to DownloadDataTaskAsync. 4. The UI thread continues into DownloadDataTaskAsync, which starts the download and returns a Task. 5. The UI thread leaves DownloadDataTaskAsync, and reaches the await in GetFaviconAsyncAsync. 6. The current SynchronizationContext is captured—it’s the UI thread. 7. GetFaviconAsync is paused by the await, and the Task from DownloadDataTask Async is told to resume it when done (with the captured SynchronizationContext). 8. The UI thread leaves GetFaviconAsync, which returned a Task, and reaches the await in GetButton_OnClick. 9.Similarly, GetButton_OnClick is paused by the await. 10. The UI thread leaves GetButton_OnClick, and is freed to work on other user actions. 11. The download finishes, so the IO completion port queues the logic in DownloadDataTaskAsync to handle that. 12. The IO completion port thread sets the Task that was returned from DownloadData TaskAsync to complete. 13. The IO completion port thread runs code inside Task to handle completion, which calls Post on the captured SynchronizationContext (the UI thread) to continue. 14. The IO completion port thread is freed to work on other IO. 15. The UI thread finds the Posted instruction and resumes GetFaviconAsync, executing the second half of it, to the end. 16. As the UI thread leaves GetFaviconAsync, it sets the Task that was returned by GetFaviconAsync to complete. 17. Because this time, the current SynchronizationContext is the same as the captured one, no Post is needed, and the UI thread proceeds synchronously. 18. The UI thread resumes GetButton_OnClick, executing the second half of it, to the end.