SlideShare a Scribd company logo
http://www.slideshare.net/martenrange/
Mårten Rånge 
Ericsson AB 
@marten_range
Concurrency 
Examples for .NET
Concurrency - responsiveness in .NET
Responsive
Performance 
Scalable algorithms
Three pillars of Concurrency 
 Scalability (CPU) 
 Parallel.For 
 Responsiveness 
 Task 
 async/await 
 Consistency 
 lock 
 Interlocked.* 
 Mutex/Event/Semaphore 
 Monitor
Responsiveness
Concurrency - responsiveness in .NET
string ReadSomeText (string fileName) 
{ 
using (var sr = new StreamReader(fileName)) 
{ 
var result = sr.ReadToEnd (); 
return result; 
} 
} 
// Blocks the thread until IO completes 
var text = ReadSomeText ("SomeText.txt");
Asyncronous programming allows 
programs to be responsive
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync(); 
return result; 
} 
}
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync(); 
return result; 
} 
}
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync(); 
return result; 
} 
}
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
using (var sr = new StreamReader(fileName)) 
{ 
var first = await sr.ReadLineAsync(); 
var second = await sr.ReadLineAsync(); 
var third = await sr.ReadLineAsync(); 
return first + second + third; 
} 
}
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync(); 
return result; 
} 
} 
// Is this code correct? 
var readTask = ReadSomeTextAsync ("SomeText.txt"); 
var text = readTask.Result;
It depends
// Is this code correct (ignore exceptions)? 
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
++m_readingFiles; 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync(); 
--m_readingFiles; 
return result; 
} 
}
It depends
// Does both TraceTheadId() trace the same id? 
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
TraceThreadId (); 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync(); 
TraceThreadId (); 
return result; 
} 
}
It depends
What’s going on?
Your new ”best” friends 
 SynchronizationContext 
 Console apps 
 Continues on ThreadPool thread 
 WindowsFormsSynchronizationContext 
 Used by WindowsForms apps 
 Continues on the ”UI” thread 
 DispatcherSynchronizationContext 
 Used by WPF and ASP.NET apps 
 Continues on the ”same” thread
SynchronizationContext
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync(); 
return result; 
} 
} 
// Is this code correct? 
var readTask = ReadSomeTextAsync ("SomeText.txt"); 
var text = readTask.Result;
Yes
// Is this code correct (ignore exceptions)? 
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
++m_readingFiles; 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync(); 
--m_readingFiles; 
return result; 
} 
}
No
// Does both TraceTheadId() trace the same id? 
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
TraceThreadId (); 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync(); 
TraceThreadId (); 
return result; 
} 
}
No
DispatcherSynchronizationContext 
& 
WindowsFormsSynchronizationContext
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync(); 
return result; 
} 
} 
// Is this code correct? 
var readTask = ReadSomeTextAsync ("SomeText.txt"); 
var text = readTask.Result;
No
// Is this code correct (ignore exceptions)? 
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
++m_readingFiles; 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync(); 
--m_readingFiles; 
return result; 
} 
}
Yes
// Does both TraceTheadId() trace the same id? 
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
TraceThreadId (); 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync(); 
TraceThreadId (); 
return result; 
} 
}
Yes
So…
SynchronizationContext 
 Is ”invisible” 
 Is a thread-global state 
 Impacts the behavior of your code significantly 
 As an application developer you can make assumptions 
 As a library developer you can’t make assumptions
ConfigureAwait
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync() 
.ConfigureAwait (false); 
return result; 
} 
} 
// Is this code correct? 
var readTask = ReadSomeTextAsync ("SomeText.txt"); 
var text = readTask.Result;
Yes
// Is this code correct (ignore exceptions)? 
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
++m_readingFiles; 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync() 
.ConfigureAwait (false); 
--m_readingFiles; 
return result; 
} 
}
No
// Does both TraceTheadId() trace the same id? 
async Task<string> ReadSomeTextAsync(string fileName) 
{ 
TraceThreadId (); 
using (var sr = new StreamReader(fileName)) 
{ 
var result = await sr.ReadToEndAsync() 
.ConfigureAwait (false); 
TraceThreadId (); 
return result; 
} 
}
No
async/await tries to be what we want
async/await reminds me of…
What we need 
 Do one thing 
 Responsiveness 
 Predictable semantics 
 Continuation is executed by a thread-pool thread 
 Visibility 
 Thread-switching should be visible in code
F# async 
// Focuses on responsiveness 
let gameLoop = 
async { 
// Switching to new thread is explicit 
do! Async.SwitchToNewThread () 
while true do 
// let! is like await in C# 
let! messages = fromVisual.AsyncDequeue 1000 
for message in messages do 
processMessage message 
}
C# 
 yield 
 Special support for enumerators 
 LINQ 
 Special support for SQL-like syntax 
 async/await 
 Special support for asynchronous programming
F# 
 seq { for x in 0..10 -> i } 
 Enumerators implemented using Computation Expressions 
 query { for c in db.Customers do select c } 
 SQL-like syntax implemented using Computation Expressions 
 async { let! r=fromVisual.AsyncDequeue 1000 in r } 
 Asynchronous programming using Computation Expressions
Computation Expressions
With async/await always consider the…
SynchronizationContext 
SynchronizationContext 
SynchronizationContext 
SynchronizationContext 
SynchronizationContext 
SynchronizationContext 
SynchronizationContext 
SynchronizationContext 
SynchronizationContext 
SynchronizationContext 
SynchronizationContext
Mårten Rånge 
Ericsson AB 
@marten_range
Links 
 Presentation 
 http://www.slideshare.net/martenrange/ 
 Code 
 https://github.com/mrange/presentations/ 
 DIY asynchronous workflows in F# 
 http://mrange.wordpress.com/2014/06/12/diy-asynchronous-workflows- 
in-f/ 
 Asynchronous programming with async/await 
 http://msdn.microsoft.com/en-us/library/hh191443.aspx

More Related Content

PPTX
Asynchronous Programming in ASP.NET
PPTX
Async in .NET
PPTX
PDF
MPI - 1
PPTX
Lzw algorithm
PDF
#Pharo Days 2016 Data Formats and Protocols
PDF
Introduction to the New Asynchronous API in the .NET Driver
PPT
what every web and app developer should know about multithreading
Asynchronous Programming in ASP.NET
Async in .NET
MPI - 1
Lzw algorithm
#Pharo Days 2016 Data Formats and Protocols
Introduction to the New Asynchronous API in the .NET Driver
what every web and app developer should know about multithreading

What's hot (6)

PPT
Network programming1
PPTX
Compiler Design
PDF
Tcpsockets
PDF
Asynchronous, Event-driven Network Application Development with Netty
DOCX
692015 programming assignment 1 building a multi­threaded w
PPTX
AMC Minor Technical Issues
Network programming1
Compiler Design
Tcpsockets
Asynchronous, Event-driven Network Application Development with Netty
692015 programming assignment 1 building a multi­threaded w
AMC Minor Technical Issues
Ad

Similar to Concurrency - responsiveness in .NET (20)

PDF
I see deadlocks : Matt Ellis - Techorama NL 2024
PPTX
History of asynchronous in .NET
PPTX
Async Programming in C# 5
PPSX
Async-await best practices in 10 minutes
PPTX
Binary Studio Academy: Concurrency in C# 5.0
PDF
Async await...oh wait!
PPTX
C# 5 deep drive into asynchronous programming
PPTX
.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund
PDF
Async Await for Mobile Apps
PPTX
Ddd melbourne 2011 C# async ctp
PDF
Deep Dive async/await in Unity with UniTask(EN)
PDF
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
PPTX
NDC Sydney 2019 - Async Demystified -- Karel Zikmund
PPTX
Async CTP 3 Presentation for MUGH 2012
PDF
Concurrecny inf sharp
PPTX
Task parallel library presentation
PDF
Why async matters
DOCX
Asynchronyin net
PPTX
Async await
PPTX
Async/Await
I see deadlocks : Matt Ellis - Techorama NL 2024
History of asynchronous in .NET
Async Programming in C# 5
Async-await best practices in 10 minutes
Binary Studio Academy: Concurrency in C# 5.0
Async await...oh wait!
C# 5 deep drive into asynchronous programming
.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund
Async Await for Mobile Apps
Ddd melbourne 2011 C# async ctp
Deep Dive async/await in Unity with UniTask(EN)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
NDC Sydney 2019 - Async Demystified -- Karel Zikmund
Async CTP 3 Presentation for MUGH 2012
Concurrecny inf sharp
Task parallel library presentation
Why async matters
Asynchronyin net
Async await
Async/Await
Ad

More from Mårten Rånge (10)

PPTX
Know your FOSS obligations
PPTX
Ray Marching Explained
PPTX
Better performance through Superscalarity
PPTX
Property Based Tesing
PPTX
Monad - a functional design pattern
PPTX
Formlets
PPTX
Pragmatic metaprogramming
PPTX
Meta Programming
PPTX
Concurrency scalability
PPTX
Concurrency
Know your FOSS obligations
Ray Marching Explained
Better performance through Superscalarity
Property Based Tesing
Monad - a functional design pattern
Formlets
Pragmatic metaprogramming
Meta Programming
Concurrency scalability
Concurrency

Recently uploaded (20)

PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
System and Network Administration Chapter 2
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
System and Network Administraation Chapter 3
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Essential Infomation Tech presentation.pptx
PDF
top salesforce developer skills in 2025.pdf
PDF
Nekopoi APK 2025 free lastest update
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Wondershare Filmora 15 Crack With Activation Key [2025
System and Network Administration Chapter 2
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Softaken Excel to vCard Converter Software.pdf
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
System and Network Administraation Chapter 3
CHAPTER 2 - PM Management and IT Context
Odoo POS Development Services by CandidRoot Solutions
Understanding Forklifts - TECH EHS Solution
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
wealthsignaloriginal-com-DS-text-... (1).pdf
Essential Infomation Tech presentation.pptx
top salesforce developer skills in 2025.pdf
Nekopoi APK 2025 free lastest update

Concurrency - responsiveness in .NET

  • 2. Mårten Rånge Ericsson AB @marten_range
  • 7. Three pillars of Concurrency  Scalability (CPU)  Parallel.For  Responsiveness  Task  async/await  Consistency  lock  Interlocked.*  Mutex/Event/Semaphore  Monitor
  • 10. string ReadSomeText (string fileName) { using (var sr = new StreamReader(fileName)) { var result = sr.ReadToEnd (); return result; } } // Blocks the thread until IO completes var text = ReadSomeText ("SomeText.txt");
  • 11. Asyncronous programming allows programs to be responsive
  • 12. async Task<string> ReadSomeTextAsync(string fileName) { using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync(); return result; } }
  • 13. async Task<string> ReadSomeTextAsync(string fileName) { using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync(); return result; } }
  • 14. async Task<string> ReadSomeTextAsync(string fileName) { using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync(); return result; } }
  • 15. async Task<string> ReadSomeTextAsync(string fileName) { using (var sr = new StreamReader(fileName)) { var first = await sr.ReadLineAsync(); var second = await sr.ReadLineAsync(); var third = await sr.ReadLineAsync(); return first + second + third; } }
  • 16. async Task<string> ReadSomeTextAsync(string fileName) { using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync(); return result; } } // Is this code correct? var readTask = ReadSomeTextAsync ("SomeText.txt"); var text = readTask.Result;
  • 18. // Is this code correct (ignore exceptions)? async Task<string> ReadSomeTextAsync(string fileName) { ++m_readingFiles; using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync(); --m_readingFiles; return result; } }
  • 20. // Does both TraceTheadId() trace the same id? async Task<string> ReadSomeTextAsync(string fileName) { TraceThreadId (); using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync(); TraceThreadId (); return result; } }
  • 23. Your new ”best” friends  SynchronizationContext  Console apps  Continues on ThreadPool thread  WindowsFormsSynchronizationContext  Used by WindowsForms apps  Continues on the ”UI” thread  DispatcherSynchronizationContext  Used by WPF and ASP.NET apps  Continues on the ”same” thread
  • 25. async Task<string> ReadSomeTextAsync(string fileName) { using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync(); return result; } } // Is this code correct? var readTask = ReadSomeTextAsync ("SomeText.txt"); var text = readTask.Result;
  • 26. Yes
  • 27. // Is this code correct (ignore exceptions)? async Task<string> ReadSomeTextAsync(string fileName) { ++m_readingFiles; using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync(); --m_readingFiles; return result; } }
  • 28. No
  • 29. // Does both TraceTheadId() trace the same id? async Task<string> ReadSomeTextAsync(string fileName) { TraceThreadId (); using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync(); TraceThreadId (); return result; } }
  • 30. No
  • 32. async Task<string> ReadSomeTextAsync(string fileName) { using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync(); return result; } } // Is this code correct? var readTask = ReadSomeTextAsync ("SomeText.txt"); var text = readTask.Result;
  • 33. No
  • 34. // Is this code correct (ignore exceptions)? async Task<string> ReadSomeTextAsync(string fileName) { ++m_readingFiles; using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync(); --m_readingFiles; return result; } }
  • 35. Yes
  • 36. // Does both TraceTheadId() trace the same id? async Task<string> ReadSomeTextAsync(string fileName) { TraceThreadId (); using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync(); TraceThreadId (); return result; } }
  • 37. Yes
  • 38. So…
  • 39. SynchronizationContext  Is ”invisible”  Is a thread-global state  Impacts the behavior of your code significantly  As an application developer you can make assumptions  As a library developer you can’t make assumptions
  • 41. async Task<string> ReadSomeTextAsync(string fileName) { using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync() .ConfigureAwait (false); return result; } } // Is this code correct? var readTask = ReadSomeTextAsync ("SomeText.txt"); var text = readTask.Result;
  • 42. Yes
  • 43. // Is this code correct (ignore exceptions)? async Task<string> ReadSomeTextAsync(string fileName) { ++m_readingFiles; using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync() .ConfigureAwait (false); --m_readingFiles; return result; } }
  • 44. No
  • 45. // Does both TraceTheadId() trace the same id? async Task<string> ReadSomeTextAsync(string fileName) { TraceThreadId (); using (var sr = new StreamReader(fileName)) { var result = await sr.ReadToEndAsync() .ConfigureAwait (false); TraceThreadId (); return result; } }
  • 46. No
  • 47. async/await tries to be what we want
  • 49. What we need  Do one thing  Responsiveness  Predictable semantics  Continuation is executed by a thread-pool thread  Visibility  Thread-switching should be visible in code
  • 50. F# async // Focuses on responsiveness let gameLoop = async { // Switching to new thread is explicit do! Async.SwitchToNewThread () while true do // let! is like await in C# let! messages = fromVisual.AsyncDequeue 1000 for message in messages do processMessage message }
  • 51. C#  yield  Special support for enumerators  LINQ  Special support for SQL-like syntax  async/await  Special support for asynchronous programming
  • 52. F#  seq { for x in 0..10 -> i }  Enumerators implemented using Computation Expressions  query { for c in db.Customers do select c }  SQL-like syntax implemented using Computation Expressions  async { let! r=fromVisual.AsyncDequeue 1000 in r }  Asynchronous programming using Computation Expressions
  • 54. With async/await always consider the…
  • 55. SynchronizationContext SynchronizationContext SynchronizationContext SynchronizationContext SynchronizationContext SynchronizationContext SynchronizationContext SynchronizationContext SynchronizationContext SynchronizationContext SynchronizationContext
  • 56. Mårten Rånge Ericsson AB @marten_range
  • 57. Links  Presentation  http://www.slideshare.net/martenrange/  Code  https://github.com/mrange/presentations/  DIY asynchronous workflows in F#  http://mrange.wordpress.com/2014/06/12/diy-asynchronous-workflows- in-f/  Asynchronous programming with async/await  http://msdn.microsoft.com/en-us/library/hh191443.aspx