Asynchronous Programming in C#

9
Asynchronous Programming in C# presented by Noor

description

Asynchronous Programming in C#

Transcript of Asynchronous Programming in C#

Asynchronous Programming in C#presented by

Noor

Parallelism vs MultithreadingParallel ComputingMultiple calculations are carried out simultaneously

Multithreaded ComputingEach program has a single threadA 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 AsynchronousSynchronousProgram is executed line by line, one line at a timeEach time a function is called, program execution waits until that function returns before continuing to the next line of codeBlocking operationAsynchronousProgram execution doesnt wait for the function to finish Instead it executes the next line of codeNon-blocking operation

Asynchronous ProgrammingAvailable in .NET 4.5 (VS 2013)It doesnt block the current threadIt doesnt create a new thread to do its operationNo relation with Parallel Computing Multithreading

Thread implementations in C#BackgroundWorker (popular in Win Forms and WPF)ThreadPoolTPL (new technology)Problem: thread is just sitting and waiting for ongoing operationsAsynchronous programmingNo need to create a new threadThe single thread can do useful work rather than waiting for the blocking operation to finishImprove responsiveness for Blocking Operation

Asynchronous Programming in C#Two keywordsasyncawaitasync is used in front of method declarationA method declared as async has one or more await operations

async Task methodAsync() {int value = await VeryBigJobAsync();

return value;}

Control flow1. async Task methodA_Async()2. {3.int a = 10;4.Task lengthTask = methodB_Async();5.int val1 = findTitleLength();6.int val2 = await lengthTask;7.return val1 + val2;8. }9. async Task methodB_Async()10.{11.HttpClient client = new HttpClient();12.Task 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 work26.}

Control flow1. async Task methodA_Async()2. {3. int a = 10;4. Task lengthTask = methodB_Async();5. int val1 = findTitleLength();6. int val2 = await lengthTask;7.return val1 + val2;8. }9. async Task methodB_Async()10.{11.HttpClient client = new HttpClient();12.Task 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 work26.}12356

7

8

9

104To the caller11131215

141. Program executes a = 102. Calls methodB_Async() 3. Creates an instance of HttpClient4. GetStringAsync() takes time to connect to the website. Instead of blocking, it passes control to the methodB_Async()5. Calls doIndependentWork() function6. Executes DoIndependentWork() function7. Control goes back to methodB_Async() function8. When it finds await operator in line 14, it passes control to MethodA_Async9. Calls findTtitleLength()10. Control goes back to methodA_Async()11. When it finds await operator in line 6, it passes control to the caller12. GetStringAsync() is completed and await operator in line 14 retrieves the result and stores in content variable.13. Executes val = content.Length14. Returns result to methodA_Async and execution strats from line 6.15. MethodA_Async() method returns val1+val2