0% found this document useful (0 votes)
382 views

Asynchronous Programming in C#

1. The document discusses asynchronous programming in C# compared to synchronous and parallel programming. 2. Asynchronous programming uses the async and await keywords to allow methods to return control to the caller before completing asynchronous operations, improving responsiveness for blocking operations without creating new threads. 3. The control flow example shows how async and await allow methodA_Async to call methodB_Async asynchronously and retrieve the result without blocking, by passing control back to the caller during I/O operations rather than waiting idly for them to complete.

Uploaded by

WebDeveloper777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
382 views

Asynchronous Programming in C#

1. The document discusses asynchronous programming in C# compared to synchronous and parallel programming. 2. Asynchronous programming uses the async and await keywords to allow methods to return control to the caller before completing asynchronous operations, improving responsiveness for blocking operations without creating new threads. 3. The control flow example shows how async and await allow methodA_Async to call methodB_Async asynchronously and retrieve the result without blocking, by passing control back to the caller during I/O operations rather than waiting idly for them to complete.

Uploaded by

WebDeveloper777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

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)

Problem: thread is just sitting and waiting for


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

async Task<int> methodAsync()


{
int value = await VeryBigJobAsync();

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

You might also like