Asynchronous Programming in C#
Asynchronous Programming in C#
presented by
Noor
Parallelism vs Multithreading
Parallel Computing
Multiple calculations are carried out
simultaneously
Multithreaded Computing
Each program has a single thread
A thread can create another thread
Multiple threads can do multiple operations
simultaneously
Parallel Computing is a paradigm and Multithreading is one of its
implementations
Synchronous vs Asynchronous
Synchronous
Program is executed line by line, one line at a time
Each time a function is called, program execution
waits until that function returns before continuing
to the next line of code
Blocking operation
Asynchronous
Program execution doesn’t wait for the function to
finish
Instead it executes the next line of code
Non-blocking operation
Asynchronous Programming
Available in .NET 4.5 (VS 2013)
It doesn’t block the current thread
It doesn’t create a new thread to do its
operation
No relation with
Parallel Computing
Multithreading
Improve responsiveness for Blocking Operation
Thread implementations in C#
BackgroundWorker (popular in Win Forms and
WPF)
ThreadPool
TPL (new technology)
return value;
}
Control flow
1. async Task<int> methodA_Async()
2. {
3. int a = 10;
4. Task<int> lengthTask = methodB_Async();
5. int val1 = findTitleLength();
6. int val2 = await lengthTask;
7. return val1 + val2;
8. }
9. async Task<int> methodB_Async()
10.{
11. HttpClient client = new HttpClient();
12. Task<string> getStringTask = client.GetStringAsync("www.bbc.com");
13. DoIndependentWork();
14. string content = await getStringTask;
15. int val = content.Length;
16. return val;
17.}
18.int findTitleLength();
19.{
20. var s = "www.bbc.com";
21. return s.Length;
22.}
23.void DoIndependentWork()
24.{
25. // some work
26.}
Control flow To the
1. async Task<int> methodA_Async()
2. { 1 caller
3. int a = 10; 1 1
4. Task<int> lengthTask = methodB_Async();
2
5. int val1 = findTitleLength();
8 6. int val2 = await lengthTask;
7. return val1 + val2; 15 9
8. }
9. async Task<int> methodB_Async()
10.{
11. HttpClient client = new HttpClient(); 3
12. Task<string> getStringTask = client.GetStringAsync("www.bbc.com"); 4
13. DoIndependentWork();
5
14. string content = await getStringTask;
15. int val = content.Length; 12
16. return val;
13
17.} 14
18.int findTitleLength();
19.{
20. 1 var s = "www.bbc.com";
21. return s.Length;
22.}
0
23.void DoIndependentWork()
24.{ 11.
14.
8.
4. Whenititfinds
Returns
When findsawait
result
GetStringAsync() await
totakesoperator
methodA_Async
operator inin lineand
line
time function
to connect 6, to
14, itexecution
it passes
passes
the control
strats
control
website.
10.
13.
9.
12.
1.
2.
3.
5.
6.
7.
15. Control
Executes
Calls
Creates
Calls
Executes
Control goes
val
GetStringAsync()
Program =back
executescontent.Length
methodB_Async() isto
findTtitleLength()
a
doIndependentWork()
goes
an methodA_Async()
completed
= 10
DoIndependentWork()
instance
MethodA_Async()back toof and
methodB_Async()
HttpClient
method returns await
function operator
function
val1+val2 in
25. // some work to
to the
from caller
line
of6.
MethodA_Async
Instead blocking, it passes control to the methodB_Async()
line 14 retrieves the result and stores in content variable.
26.} 6
7