SlideShare a Scribd company logo
Sync with Async 
M Prabath Maduranga Peiris 
MSP / MSA 
RUSL
Why Async ? 
Async allows programmer to avoid performance 
bottlenecks and improve applications responsiveness. 
Scenario : 
Applications access the HTTP (Web ) inside the program 
Access the web is slow and delayed 
Total application will be wait 
Sync With Async - PrabathSL
Sync With Async - PrabathSL 
private void uploadImageCallbackHell() 
{ 
bool BufferStatus = true; 
var filePicker = new FilePicker(); 
var PickerTask = filePicker.PickImage(); 
PickerTask.ContinueWith((PickerReturn) => 
{ 
if (PickerTask.IsCanceled) 
{ 
BufferStatus = false; 
} 
else 
{ 
var Uploadtask = new Task<bool>(() => 
{ 
return sendUpdate(PickerReturn); 
}); 
Uploadtask.ContinueWith((UploadReturnTask)()=> 
{ 
BufferStatus = false; 
}); 
} 
} 
); 
}
With Async Application can continue with other works that 
doesn’t depend on blocked resource until the blocked task 
finished. 
Sync With Async - PrabathSL
Async 
Async programming available with Visual Studio 2012 
and .NET framework 4.5 onwards 
Runtime components support Async: 
Web ( HttpClient / SyndicationClient ) 
File Access ( StorageFile / StreamWriter / XmlReader ) 
Images ( MediaCapture / BitMapEncoder and Decoder ) 
Sync With Async - PrabathSL
Concepts in Async 
Sync With Async - PrabathSL
Tasks 
Reasoning about Background operations 
Task and Task<T> 
Encapsulate units of works 
State : Running , Finished , Cancelled 
Result 
Thrown Exceptions 
Sync With Async - PrabathSL
Working With Tasks 
Useful Operations 
Wait : Task.WaitAny(t1,t2,t3) 
Wait : Task.WaitAll(t1,t1,t3) 
Chain : task1.ContinueWith(task2) 
Wrap : Task.WhenAny(t1,t2,t3) 
Wrap : Task.WhenAll(t1,t2,t3) 
Sync With Async - PrabathSL
Async Support 
Base Class Libraries (BCL) 
All standard Async API’s from .NET BCL 
Coverage is .NET 4.5 Complete for BCL 
What is available on Async 
Any API that take more than 50ms to run 
Sync With Async - PrabathSL
Compiler is Amazing 
async key word informs the compiler that this method need 
to be handle with it self 
await indicates a suspension point where callback need to 
be generated 
Continuations are generated after each suspension point 
Exception handling with Try Catch 
Remember async and await , then use Tasks 
Sync With Async - PrabathSL
How to use Async ? 
private async void ButtonDefinition() 
{ 
Async modifier can be on , 
Methods 
Lambdas 
Anonymous methods 
button.Click += async (sender, e) =>{ 
int number = await getNumberAsync(); 
Use Coding standards 
Use Async Suffix | e.g. DownloadAsync 
Return Task or Task<T> according to situation 
void for only event handlers 
Sync With Async - PrabathSL 
}; 
Task<int> itemNumberTask = getNumberAsync(); 
int num = await itemNumberTask; 
} 
private async Task<int> getNumberAsync() 
{ 
await Task.Delay(200); 
return 10; 
}
Demo 
Sync With Async - PrabathSL
Async Execution 
1 
2 
3 
private void independentWork() 
Sync With Async - PrabathSL
Advanced Tips 
Working with interdependent Async Operations ?? 
Mutual exclusion 
Shared flag 
Sleep and Wakeup 
Mutex 
Semaphore 
Sync With Async - PrabathSL
Advanced Tips 
Task manages set of class 
Task.WhenAll(Ienumerable<Task>) 
Require all class to complete 
Task.WhenAny(Ienumerable<Task>) 
Require any one of task completed 
private async Task BulkTaskOperationAsync() 
Sync With Async - PrabathSL 
{ 
var t1 = await Task.Factory.StartNew(() => getNumberAsync()); 
var t2 = await Task.Factory.StartNew(() => getNumberAsync()); 
Task.WaitAny(t1, t2); 
}
Questions 
Sync With Async - PrabathSL
Thank You 
Blog : prabathsl.blogspot.com 
Email : prabathsl@outlook.com 
Fb/ Twitter : @mpmpeiris 
Sync With Async - PrabathSL

More Related Content

PDF
Effective java item 80 and 81
PPTX
C# 5 deep drive into asynchronous programming
PDF
Async/Await: TPL & Message Pumps
PPTX
Asynchronous Programming in ASP.NET
PDF
Async/Await Best Practices
PPT
Evolution of asynchrony in (ASP).NET
PDF
Reactive programming using rx java & akka actors - pdx-scala - june 2014
PPTX
Intro to Functional Programming with RxJava
Effective java item 80 and 81
C# 5 deep drive into asynchronous programming
Async/Await: TPL & Message Pumps
Asynchronous Programming in ASP.NET
Async/Await Best Practices
Evolution of asynchrony in (ASP).NET
Reactive programming using rx java & akka actors - pdx-scala - june 2014
Intro to Functional Programming with RxJava

What's hot (19)

PPTX
Async Programming in C# 5
PPT
C# Async on iOS and Android - Craig Dunn, Developer Evangelist at Xamarin
PPTX
C# Async on iOS and Android - Miguel de Icaza, CTO of Xamarin
PPTX
Reactive programming for java developers
PPTX
Top 10 RxJs Operators in Angular
PPTX
Reactive
PDF
Reduce dependency on Rx with Kotlin Coroutines
PDF
MongoDB World 2016: Implementing Async Networking in MongoDB 3.2
PDF
Przywitaj się z reactive extensions
PDF
Runtime Bytecode Transformation for Smalltalk
PDF
Business Dashboards using Bonobo ETL, Grafana and Apache Airflow
PDF
Functional Programming with JavaScript
PDF
Monitoring Kafka w/ Prometheus
PDF
My Gentle Introduction to RxJS
PPTX
Reactive Programming and RxJS
PPTX
Tamir Dresher - Async Streams in C#
PPTX
Introduction to RxJava on Android
PDF
RxJava@Android
PPTX
Monitoring on Kubernetes using prometheus
Async Programming in C# 5
C# Async on iOS and Android - Craig Dunn, Developer Evangelist at Xamarin
C# Async on iOS and Android - Miguel de Icaza, CTO of Xamarin
Reactive programming for java developers
Top 10 RxJs Operators in Angular
Reactive
Reduce dependency on Rx with Kotlin Coroutines
MongoDB World 2016: Implementing Async Networking in MongoDB 3.2
Przywitaj się z reactive extensions
Runtime Bytecode Transformation for Smalltalk
Business Dashboards using Bonobo ETL, Grafana and Apache Airflow
Functional Programming with JavaScript
Monitoring Kafka w/ Prometheus
My Gentle Introduction to RxJS
Reactive Programming and RxJS
Tamir Dresher - Async Streams in C#
Introduction to RxJava on Android
RxJava@Android
Monitoring on Kubernetes using prometheus
Ad

Viewers also liked (18)

PPTX
From Callback Hell to Async Heaven - Promises!
PDF
Sync async-blocking-nonblocking-io
PPTX
Avoiding Callback Hell with Async.js
PPTX
Avoiding callback hell in Node js using promises
PPTX
Async and Await on the Server
PPTX
Everything you wanted to know about writing async, concurrent http apps in java
PDF
Lviv MD Day 2015 Олексій Демедецький "Using Future for async flow application...
PDF
How to meets Async and Task
PDF
Promises And Chaining In AngularJS - Into Callback Hell And Back Again
PDF
Using Async in your Mobile Apps - Marek Safar
PPT
Multithreading, Blocking IO and Async IO
PDF
Async await...oh wait!
PPTX
KGC 2014: 분산 게임 서버 구조론
ODP
Servlet 3.1 Async I/O
PDF
Tcp ip & io model
PDF
Callbacks, promises, generators - asynchronous javascript
PPTX
Think Async in Java 8
PDF
Syncing Async
From Callback Hell to Async Heaven - Promises!
Sync async-blocking-nonblocking-io
Avoiding Callback Hell with Async.js
Avoiding callback hell in Node js using promises
Async and Await on the Server
Everything you wanted to know about writing async, concurrent http apps in java
Lviv MD Day 2015 Олексій Демедецький "Using Future for async flow application...
How to meets Async and Task
Promises And Chaining In AngularJS - Into Callback Hell And Back Again
Using Async in your Mobile Apps - Marek Safar
Multithreading, Blocking IO and Async IO
Async await...oh wait!
KGC 2014: 분산 게임 서버 구조론
Servlet 3.1 Async I/O
Tcp ip & io model
Callbacks, promises, generators - asynchronous javascript
Think Async in Java 8
Syncing Async
Ad

Similar to Sync with async (20)

PPSX
Async-await best practices in 10 minutes
PPTX
Training – Going Async
PDF
Async Await for Mobile Apps
PPTX
Asynchronous Programming in .NET
PDF
Why async matters
PPTX
End to-end async and await
PPTX
Ordina SOFTC Presentation - Async CTP
PPTX
History of asynchronous in .NET
PDF
Webcast: Asynchronous Programming Demystified
PPTX
Async programming in c#
PPTX
Async CTP 3 Presentation for MUGH 2012
PPTX
Windows Phone 8 - 3.5 Async Programming
PPTX
C#: Understanding ConfigureAwait(false)
PPTX
Workshop: Async and Parallel in C#
PPTX
Binary Studio Academy: Concurrency in C# 5.0
PPTX
Async in C# - The Good, the Bad and the Ugly
PPTX
Welcome to an asynchronous world 1.29s
PPTX
Asynchronous programming - .NET Way
PPTX
Async in .NET
KEY
Asynchronous Programming
Async-await best practices in 10 minutes
Training – Going Async
Async Await for Mobile Apps
Asynchronous Programming in .NET
Why async matters
End to-end async and await
Ordina SOFTC Presentation - Async CTP
History of asynchronous in .NET
Webcast: Asynchronous Programming Demystified
Async programming in c#
Async CTP 3 Presentation for MUGH 2012
Windows Phone 8 - 3.5 Async Programming
C#: Understanding ConfigureAwait(false)
Workshop: Async and Parallel in C#
Binary Studio Academy: Concurrency in C# 5.0
Async in C# - The Good, the Bad and the Ugly
Welcome to an asynchronous world 1.29s
Asynchronous programming - .NET Way
Async in .NET
Asynchronous Programming

Recently uploaded (20)

PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
A Presentation on Artificial Intelligence
PDF
Encapsulation theory and applications.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPT
Teaching material agriculture food technology
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Cloud computing and distributed systems.
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
Big Data Technologies - Introduction.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
Diabetes mellitus diagnosis method based random forest with bat algorithm
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Review of recent advances in non-invasive hemoglobin estimation
A Presentation on Artificial Intelligence
Encapsulation theory and applications.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Building Integrated photovoltaic BIPV_UPV.pdf
Spectral efficient network and resource selection model in 5G networks
Dropbox Q2 2025 Financial Results & Investor Presentation
Network Security Unit 5.pdf for BCA BBA.
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Teaching material agriculture food technology
Digital-Transformation-Roadmap-for-Companies.pptx
Cloud computing and distributed systems.
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Big Data Technologies - Introduction.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
20250228 LYD VKU AI Blended-Learning.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?

Sync with async

  • 1. Sync with Async M Prabath Maduranga Peiris MSP / MSA RUSL
  • 2. Why Async ? Async allows programmer to avoid performance bottlenecks and improve applications responsiveness. Scenario : Applications access the HTTP (Web ) inside the program Access the web is slow and delayed Total application will be wait Sync With Async - PrabathSL
  • 3. Sync With Async - PrabathSL private void uploadImageCallbackHell() { bool BufferStatus = true; var filePicker = new FilePicker(); var PickerTask = filePicker.PickImage(); PickerTask.ContinueWith((PickerReturn) => { if (PickerTask.IsCanceled) { BufferStatus = false; } else { var Uploadtask = new Task<bool>(() => { return sendUpdate(PickerReturn); }); Uploadtask.ContinueWith((UploadReturnTask)()=> { BufferStatus = false; }); } } ); }
  • 4. With Async Application can continue with other works that doesn’t depend on blocked resource until the blocked task finished. Sync With Async - PrabathSL
  • 5. Async Async programming available with Visual Studio 2012 and .NET framework 4.5 onwards Runtime components support Async: Web ( HttpClient / SyndicationClient ) File Access ( StorageFile / StreamWriter / XmlReader ) Images ( MediaCapture / BitMapEncoder and Decoder ) Sync With Async - PrabathSL
  • 6. Concepts in Async Sync With Async - PrabathSL
  • 7. Tasks Reasoning about Background operations Task and Task<T> Encapsulate units of works State : Running , Finished , Cancelled Result Thrown Exceptions Sync With Async - PrabathSL
  • 8. Working With Tasks Useful Operations Wait : Task.WaitAny(t1,t2,t3) Wait : Task.WaitAll(t1,t1,t3) Chain : task1.ContinueWith(task2) Wrap : Task.WhenAny(t1,t2,t3) Wrap : Task.WhenAll(t1,t2,t3) Sync With Async - PrabathSL
  • 9. Async Support Base Class Libraries (BCL) All standard Async API’s from .NET BCL Coverage is .NET 4.5 Complete for BCL What is available on Async Any API that take more than 50ms to run Sync With Async - PrabathSL
  • 10. Compiler is Amazing async key word informs the compiler that this method need to be handle with it self await indicates a suspension point where callback need to be generated Continuations are generated after each suspension point Exception handling with Try Catch Remember async and await , then use Tasks Sync With Async - PrabathSL
  • 11. How to use Async ? private async void ButtonDefinition() { Async modifier can be on , Methods Lambdas Anonymous methods button.Click += async (sender, e) =>{ int number = await getNumberAsync(); Use Coding standards Use Async Suffix | e.g. DownloadAsync Return Task or Task<T> according to situation void for only event handlers Sync With Async - PrabathSL }; Task<int> itemNumberTask = getNumberAsync(); int num = await itemNumberTask; } private async Task<int> getNumberAsync() { await Task.Delay(200); return 10; }
  • 12. Demo Sync With Async - PrabathSL
  • 13. Async Execution 1 2 3 private void independentWork() Sync With Async - PrabathSL
  • 14. Advanced Tips Working with interdependent Async Operations ?? Mutual exclusion Shared flag Sleep and Wakeup Mutex Semaphore Sync With Async - PrabathSL
  • 15. Advanced Tips Task manages set of class Task.WhenAll(Ienumerable<Task>) Require all class to complete Task.WhenAny(Ienumerable<Task>) Require any one of task completed private async Task BulkTaskOperationAsync() Sync With Async - PrabathSL { var t1 = await Task.Factory.StartNew(() => getNumberAsync()); var t2 = await Task.Factory.StartNew(() => getNumberAsync()); Task.WaitAny(t1, t2); }
  • 16. Questions Sync With Async - PrabathSL
  • 17. Thank You Blog : prabathsl.blogspot.com Email : prabathsl@outlook.com Fb/ Twitter : @mpmpeiris Sync With Async - PrabathSL

Editor's Notes

  • #3: Desire to build responsive applications With long running tasks Use background thread Need to have own handshake protocol to calling its done or not , ended up with everyone make their own notification sytems Database access or file access Network connection interference Working with call backs (node js ) pass message to other end of the day it will become more difficult to program
  • #6: Support .NET base class libraries . It means what you did with regular .NET and now Async is there
  • #8: Task is something like this , It will contains all other properties except the result. State , exceptions etc. All the errors that thrown win the task execution is thrown after await is done.
  • #9: Say to wait until any one of these are finished Task 2 starts just after task1 (once file download you can convert it into another format) Task encapsulates those tasks and any of them are finished main task is finished (getting same data from multiple distributed Db’s )
  • #10: You ca use sync or async. Decide that make application better
  • #11: When await keyword hit compiler going to look at task that is waiting generate code to run that in thread and managed that and error handing
  • #12: With Task<int> method its not gonna just ended the task and finish the job. You can read the Task.result property to get int result. But Tasktast<int> itself make query the integer type result in Task.result It returning int, but signature is Task<int>. Compiler identifies the return value is int an in the execution flow it is I task and return it as Task.result at the await finished Await cant use in Catch or finally