SlideShare a Scribd company logo
Async Streams in C#
Tamir Dresher
Twitter: @tamir_dresher
2
System Architect @ @tamir_dresher
Tamir Dresher
My Books:
Software Engineering Lecturer
Ruppin Academic Center https://www.israelclouds.com/iasaisrael
https://tinyurl.com/Telescopim-YouTube
Async work in C#
.NET 2.0
- Event Asynchronous
Pattern
(Async method + event)
.NET 1.1
Asynchronous Programming Model
(Begin/End Methods)
.NET 4.5
Task Asynchronous
Pattern
C# 7.0
Async Main
C# 5.0
Async Await
C# 8
- Async Streams
- Async Disposables
Single
Value
• IObservable<T> , IObserver<T>
• Reactive Extensions (Rx)
Async Stream
g(x)
f(x)
Demo
IEnumerable and Iterators
SharpLab Demo
Sync over Async
Avoid Exposing Synchronous Wrappers for Asynchronous Implementations
Sync over async will lead to ThreadPool starvation on high load
Thread Pool
Thread
Request
Async IO
Thread
IAsyncEnumerable
namespace System.Collections.Generic
{
public interface IAsyncEnumerable<out T>
{
IAsyncEnumerator<T> GetAsyncEnumerator(
CancellationToken cancellationToken = default);
}
public interface IAsyncEnumerator<out T> : IAsyncDisposable
{
ValueTask<bool> MoveNextAsync();
T Current { get; }
}
}
SharpLab
namespace System
{
public interface IAsyncDisposable
{
ValueTask DisposeAsync();
}
}
namespace System.Collections.Generic
{
public interface IEnumerable<out T> : IEnumerable
{
IEnumerator<T> GetEnumerator();
}
public interface IEnumerator<out T> : IDisposable,
IEnumerator
{
bool MoveNext ();
T Current { get; }
}
}
Async stream example
public static async Task Main()
{
var records = ReadLines("transactions.csv");
await foreach (var record in records)
{
Console.WriteLine($"Transaction from {record[0]} to {record[1]}");
}
}
public static async IAsyncEnumerable<string[]> ReadLines(string path)
{
var lines = await System.IO.File.ReadAllLinesAsync(path);
foreach (var line in lines)
{
yield return line.Split(',');
}
}
Summary
Single Value Multiple Value
Sync T IEnumerable<T>
Async Task<T> IAsyncEnumerable<T>
IObservable<T>
Avoid as much as possible Sync-Over-Async
Cancellation - Mark the CancelleationToken parameter with EnumeratorCancellation
attribute and use the WithCancellation() extension method propagate the token
Use the System.Linq.Async and System.Interactive.Async NuGet packages to add async
LINQ operators
@tamir_dresher

More Related Content

What's hot (20)

PDF
How we use Fluentd in Treasure Data
Sadayuki Furuhashi
 
PDF
Comparison nodejs frameworks using Polls API
Ladislav Prskavec
 
PPTX
Sharding and Load Balancing in Scala - Twitter's Finagle
Geoff Ballinger
 
PDF
AWSインフラのコード化にトライしてみて
daisuke awaji
 
PDF
AOP in Python API design
meij200
 
PDF
Scaling up task processing with Celery
Nicolas Grasset
 
PDF
Phoenix for Rails Devs
Diacode
 
PDF
Tech Webinar: AUMENTARE LA SCALABILITÀ DELLE WEB APP CON SERVLET 3.1 ASYNC I/O
Codemotion
 
PDF
Celery introduction
Ionel Mărieș Cristian
 
PPTX
Alteryx SDK
James Dunkerley
 
PPTX
Flask & Flask-restx
ammaraslam18
 
PDF
Testing your infrastructure with litmus
Bram Vogelaar
 
PPTX
psCloudstack Internals
Hans van Veen
 
PPTX
Async Programming in C# 5
Pratik Khasnabis
 
PPTX
Terraform day 2
Kalkey
 
PPTX
Workshop: Async and Parallel in C#
Rainer Stropek
 
PDF
IVS CTO Night And Day 2018 Winter - [re:Cap] Serverless & Mobile
Amazon Web Services Japan
 
PPTX
History of asynchronous in .NET
Marcin Tyborowski
 
PPTX
Sync with async
prabathsl
 
PDF
Altitude NY 2018: Leveraging Log Streaming to Build the Best Dashboards, Ever
Fastly
 
How we use Fluentd in Treasure Data
Sadayuki Furuhashi
 
Comparison nodejs frameworks using Polls API
Ladislav Prskavec
 
Sharding and Load Balancing in Scala - Twitter's Finagle
Geoff Ballinger
 
AWSインフラのコード化にトライしてみて
daisuke awaji
 
AOP in Python API design
meij200
 
Scaling up task processing with Celery
Nicolas Grasset
 
Phoenix for Rails Devs
Diacode
 
Tech Webinar: AUMENTARE LA SCALABILITÀ DELLE WEB APP CON SERVLET 3.1 ASYNC I/O
Codemotion
 
Celery introduction
Ionel Mărieș Cristian
 
Alteryx SDK
James Dunkerley
 
Flask & Flask-restx
ammaraslam18
 
Testing your infrastructure with litmus
Bram Vogelaar
 
psCloudstack Internals
Hans van Veen
 
Async Programming in C# 5
Pratik Khasnabis
 
Terraform day 2
Kalkey
 
Workshop: Async and Parallel in C#
Rainer Stropek
 
IVS CTO Night And Day 2018 Winter - [re:Cap] Serverless & Mobile
Amazon Web Services Japan
 
History of asynchronous in .NET
Marcin Tyborowski
 
Sync with async
prabathsl
 
Altitude NY 2018: Leveraging Log Streaming to Build the Best Dashboards, Ever
Fastly
 

Similar to Tamir Dresher - Async Streams in C# (20)

PPTX
Binary Studio Academy: Concurrency in C# 5.0
Binary Studio
 
PPTX
Concurrency - responsiveness in .NET
Mårten Rånge
 
PPTX
Ddd melbourne 2011 C# async ctp
Pratik Khasnabis
 
PDF
Why async matters
timbc
 
PDF
Async streams
Christian Nagel
 
PDF
C# 8 in Libraries and Applications - BASTA! Frankfurt 2020
Christian Nagel
 
PDF
C# 8 in Libraries and Applications
Christian Nagel
 
PDF
C# What's next? (7.x and 8.0)
Christian Nagel
 
PDF
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
jeffz
 
PPTX
.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund
Karel Zikmund
 
PPTX
Async in .NET
RTigger
 
PPTX
Training – Going Async
Betclic Everest Group Tech Team
 
PPTX
Async programming in c#
Ahasanul Kalam Akib
 
PPTX
C# 5 deep drive into asynchronous programming
Praveen Prajapati
 
PPTX
Asynchronous programming in C#
Bohdan Pashkovskyi
 
PPTX
NDC Sydney 2019 - Async Demystified -- Karel Zikmund
Karel Zikmund
 
PDF
Deep Dive async/await in Unity with UniTask(EN)
Yoshifumi Kawai
 
PPTX
Asynchronous Programming in .NET
Pierre-Luc Maheu
 
PPTX
C# Async on iOS and Android - Miguel de Icaza, CTO of Xamarin
Xamarin
 
PPTX
Welcome to an asynchronous world 1.29s
Jose Luis Latorre Millas
 
Binary Studio Academy: Concurrency in C# 5.0
Binary Studio
 
Concurrency - responsiveness in .NET
Mårten Rånge
 
Ddd melbourne 2011 C# async ctp
Pratik Khasnabis
 
Why async matters
timbc
 
Async streams
Christian Nagel
 
C# 8 in Libraries and Applications - BASTA! Frankfurt 2020
Christian Nagel
 
C# 8 in Libraries and Applications
Christian Nagel
 
C# What's next? (7.x and 8.0)
Christian Nagel
 
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
jeffz
 
.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund
Karel Zikmund
 
Async in .NET
RTigger
 
Training – Going Async
Betclic Everest Group Tech Team
 
Async programming in c#
Ahasanul Kalam Akib
 
C# 5 deep drive into asynchronous programming
Praveen Prajapati
 
Asynchronous programming in C#
Bohdan Pashkovskyi
 
NDC Sydney 2019 - Async Demystified -- Karel Zikmund
Karel Zikmund
 
Deep Dive async/await in Unity with UniTask(EN)
Yoshifumi Kawai
 
Asynchronous Programming in .NET
Pierre-Luc Maheu
 
C# Async on iOS and Android - Miguel de Icaza, CTO of Xamarin
Xamarin
 
Welcome to an asynchronous world 1.29s
Jose Luis Latorre Millas
 
Ad

More from Tamir Dresher (20)

PPTX
Engineering tools for making smarter decisions .pptx
Tamir Dresher
 
PDF
NET Aspire - NET Conf IL 2024 - Tamir Dresher.pdf
Tamir Dresher
 
PPTX
Tamir Dresher - DotNet 7 What's new.pptx
Tamir Dresher
 
PPTX
Anatomy of a data driven architecture - Tamir Dresher
Tamir Dresher
 
PPTX
Tamir Dresher Clarizen adventures with the wild GC during the holiday season
Tamir Dresher
 
PDF
Debugging tricks you wish you knew Tamir Dresher - Odessa 2019
Tamir Dresher
 
PDF
From zero to hero with the actor model - Tamir Dresher - Odessa 2019
Tamir Dresher
 
PPTX
Tamir Dresher - Demystifying the Core of .NET Core
Tamir Dresher
 
PPTX
Breaking the monolith to microservice with Docker and Kubernetes (k8s)
Tamir Dresher
 
PPTX
.Net december 2017 updates - Tamir Dresher
Tamir Dresher
 
PPTX
Testing time and concurrency Rx
Tamir Dresher
 
PPTX
Building responsive application with Rx - confoo - tamir dresher
Tamir Dresher
 
PPTX
.NET Debugging tricks you wish you knew tamir dresher
Tamir Dresher
 
PPTX
From Zero to the Actor Model (With Akka.Net) - CodeMash2017 - Tamir Dresher
Tamir Dresher
 
PPTX
Building responsive applications with Rx - CodeMash2017 - Tamir Dresher
Tamir Dresher
 
PPTX
Debugging tricks you wish you knew - Tamir Dresher
Tamir Dresher
 
PPTX
Rx 101 - Tamir Dresher - Copenhagen .NET User Group
Tamir Dresher
 
PPTX
Cloud patterns - NDC Oslo 2016 - Tamir Dresher
Tamir Dresher
 
PPTX
Reactiveness All The Way - SW Architecture 2015 Conference
Tamir Dresher
 
PPTX
Rx 101 Codemotion Milan 2015 - Tamir Dresher
Tamir Dresher
 
Engineering tools for making smarter decisions .pptx
Tamir Dresher
 
NET Aspire - NET Conf IL 2024 - Tamir Dresher.pdf
Tamir Dresher
 
Tamir Dresher - DotNet 7 What's new.pptx
Tamir Dresher
 
Anatomy of a data driven architecture - Tamir Dresher
Tamir Dresher
 
Tamir Dresher Clarizen adventures with the wild GC during the holiday season
Tamir Dresher
 
Debugging tricks you wish you knew Tamir Dresher - Odessa 2019
Tamir Dresher
 
From zero to hero with the actor model - Tamir Dresher - Odessa 2019
Tamir Dresher
 
Tamir Dresher - Demystifying the Core of .NET Core
Tamir Dresher
 
Breaking the monolith to microservice with Docker and Kubernetes (k8s)
Tamir Dresher
 
.Net december 2017 updates - Tamir Dresher
Tamir Dresher
 
Testing time and concurrency Rx
Tamir Dresher
 
Building responsive application with Rx - confoo - tamir dresher
Tamir Dresher
 
.NET Debugging tricks you wish you knew tamir dresher
Tamir Dresher
 
From Zero to the Actor Model (With Akka.Net) - CodeMash2017 - Tamir Dresher
Tamir Dresher
 
Building responsive applications with Rx - CodeMash2017 - Tamir Dresher
Tamir Dresher
 
Debugging tricks you wish you knew - Tamir Dresher
Tamir Dresher
 
Rx 101 - Tamir Dresher - Copenhagen .NET User Group
Tamir Dresher
 
Cloud patterns - NDC Oslo 2016 - Tamir Dresher
Tamir Dresher
 
Reactiveness All The Way - SW Architecture 2015 Conference
Tamir Dresher
 
Rx 101 Codemotion Milan 2015 - Tamir Dresher
Tamir Dresher
 
Ad

Recently uploaded (20)

PDF
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PDF
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 

Tamir Dresher - Async Streams in C#

  • 1. Async Streams in C# Tamir Dresher Twitter: @tamir_dresher
  • 2. 2 System Architect @ @tamir_dresher Tamir Dresher My Books: Software Engineering Lecturer Ruppin Academic Center https://www.israelclouds.com/iasaisrael https://tinyurl.com/Telescopim-YouTube
  • 3. Async work in C# .NET 2.0 - Event Asynchronous Pattern (Async method + event) .NET 1.1 Asynchronous Programming Model (Begin/End Methods) .NET 4.5 Task Asynchronous Pattern C# 7.0 Async Main C# 5.0 Async Await C# 8 - Async Streams - Async Disposables Single Value • IObservable<T> , IObserver<T> • Reactive Extensions (Rx)
  • 6. Sync over Async Avoid Exposing Synchronous Wrappers for Asynchronous Implementations Sync over async will lead to ThreadPool starvation on high load Thread Pool Thread Request Async IO Thread
  • 7. IAsyncEnumerable namespace System.Collections.Generic { public interface IAsyncEnumerable<out T> { IAsyncEnumerator<T> GetAsyncEnumerator( CancellationToken cancellationToken = default); } public interface IAsyncEnumerator<out T> : IAsyncDisposable { ValueTask<bool> MoveNextAsync(); T Current { get; } } } SharpLab namespace System { public interface IAsyncDisposable { ValueTask DisposeAsync(); } } namespace System.Collections.Generic { public interface IEnumerable<out T> : IEnumerable { IEnumerator<T> GetEnumerator(); } public interface IEnumerator<out T> : IDisposable, IEnumerator { bool MoveNext (); T Current { get; } } }
  • 8. Async stream example public static async Task Main() { var records = ReadLines("transactions.csv"); await foreach (var record in records) { Console.WriteLine($"Transaction from {record[0]} to {record[1]}"); } } public static async IAsyncEnumerable<string[]> ReadLines(string path) { var lines = await System.IO.File.ReadAllLinesAsync(path); foreach (var line in lines) { yield return line.Split(','); } }
  • 9. Summary Single Value Multiple Value Sync T IEnumerable<T> Async Task<T> IAsyncEnumerable<T> IObservable<T> Avoid as much as possible Sync-Over-Async Cancellation - Mark the CancelleationToken parameter with EnumeratorCancellation attribute and use the WithCancellation() extension method propagate the token Use the System.Linq.Async and System.Interactive.Async NuGet packages to add async LINQ operators @tamir_dresher