Asynchronous Programming in C#
Asynchronous Programming in C#
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
Asynchronous
Program execution doesnt 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 doesnt block the current thread
It doesnt create a new thread to do its
operation
No relation with
Parallel Computing
Multithreading
ongoing operations
Asynchronous programming
No need to create a new thread
The single thread can do useful work rather
than waiting for the blocking operation to finish
Asynchronous Programming in C#
Two keywords
async
await
async is used in front of method declaration
A method declared as async has one or more await
operations
asyncTask<int>methodAsync()
{
intvalue=awaitVeryBigJobAsync();
returnvalue;
}
Control flow
1.asyncTask<int>methodA_Async()
2.{
3.
inta=10;
4.
Task<int>lengthTask=methodB_Async();
5.
intval1=findTitleLength();
6.
intval2=awaitlengthTask;
7.
returnval1+val2;
8.}
9.asyncTask<int>methodB_Async()
10.{
11. HttpClientclient=newHttpClient();
12. Task<string>getStringTask=client.GetStringAsync("www.bbc.com");
13. DoIndependentWork();
14. stringcontent=awaitgetStringTask;
15. intval=content.Length;
16. returnval;
17.}
18.intfindTitleLength();
19.{
20. vars="www.bbc.com";
21. returns.Length;
22.}
23.voidDoIndependentWork()
24.{
25. //somework
26.}
Control flow
To the
1.asyncTask<int>methodA_Async()
caller
2.{
1
3. inta=10;
1
1
2
4. Task<int>lengthTask=methodB_Async();
5. intval1=findTitleLength();
8 6. intval2=awaitlengthTask;
7.
returnval1+val2;
15
9
8.}
9.asyncTask<int>methodB_Async()
10.{
11. HttpClientclient=newHttpClient();
3
12. Task<string>getStringTask=client.GetStringAsync("www.bbc.com");
4
13. DoIndependentWork();
5
14. stringcontent=awaitgetStringTask;
12
15. intval=content.Length;
13
16. returnval;
14
17.}
18.intfindTitleLength();
19.{
20. 1 vars="www.bbc.com";
21. returns.Length;
0
22.}
23.voidDoIndependentWork()
4.
GetStringAsync()
takes
time toinconnect
to the
11.
When
it
finds
await
operator
line
6, it
12.
GetStringAsync()
isoperator
completed
and
await
7.
Control
goes
back
to
methodB_Async()
14.
Returns
result
tocontent.Length
methodA_Async
and
24.{
8.
When
it
finds
await
in
line
14,
it
9.
10.
13.
Control
Executes
findTtitleLength()
goes
val
=
back
to
methodA_Async()
1.
Program
executes
a
=
10
2.
3.
5.
6.
Creates
Calls
Executes
methodB_Async()
doIndependentWork()
an
DoIndependentWork()
instance
of
HttpClient
function
function
15.
MethodA_Async()
methodthe
returns
website.
Instead
of
blocking,
control
to
passes
the
caller
operator
instrats
lineto
14
retrieves
resultval1+val2
and
functioncontrol
execution
from
line 6. it passes
25. //somework
passes
control
to
MethodA_Async
the
methodB_Async()
6
stores
in content variable.
26.}