This document provides an overview of asynchronous programming in C#. It defines asynchronous programming as freeing the current thread while waiting for an I/O operation like a network request to complete. There are two types of asynchronous work: I/O-bound like file access which doesn't need dedicated threads, and CPU-bound like calculations which do. Asynchronous programming keeps apps responsive, improves performance by utilizing multiple cores, and avoids thread pool starvation. It is compared to synchronous programming and different asynchronous patterns are described. The benefits of asynchronous programming in web APIs to handle many concurrent requests are explained. Finally, async/await syntax and its usefulness are covered along with some drawbacks.
Introduction to asynchronous programming in C# presented by Software Engineers Ahasanul Kalam Akib and Imtiyaz Hossain.
Asynchronous programming allows freeing the current thread while handling I/O, with I/O-bound and CPU-bound operations.
Benefits include a responsive UI, improved performance, multi-core utilization, and preventing thread pool starvation.
Comparison of synchronous (blocking tasks) and asynchronous operations (multiple tasks simultaneously) and how they operate.
Different programming models: Task-based async pattern (recommended), event-based and async programming model in single vs multi-threaded environments.
Syntax for async methods and differences between blocking (synchronous) and non-blocking (asynchronous) code, with focus on await functionality.
Advantages of asynchronous calls in web API to handle high requests, freeing threads to enhance scalability and efficiency.
Complexity and maintenance issues in asynchronous code, increased memory usage, and debugging difficulties.
References for further reading on async programming techniques in .NET, followed by a thank you and Q&A session.
2 What is asynchronousprogramming? • Asynchronous means that the current thread is freed while you are waiting for a response to some I/O operation. (local storage, a network request, etc.) • A sort of parallel programming that permits a unit of labor to run separately from the first application thread. • Is a key technique that makes it straight forward to handle blocking I/O and concurrent operations on multiple cores. • Asynchronous code isn't about multi-threading. Actually the opposite: Part of the benefit of asynchronous code is to not need more threads.
3.
3 What is asynchronousprogramming? There are two types of work that are done asynchronously: I/O-bound operations could be • File-system accesses • HTTP requests, • API calls, or database queries. CPU-bound operations would be • like encrypting data • complex calculations • image or document management. Which are: • Heavy computational work • Needs continuous CPU involvement Hence, This will need a dedicated thread, and will generally use a ThreadPool thread • Which can be done without CPU system. • May not need any dedicated threads • The network or disk driver may handle it by themselves
4.
4 Benefits? • Keep theUI of our app responsive. • Can improve the performance of our application. • Utilization of multi-core systems • Avoid thread pool starvation
5.
5 Synchronous vs Asynchronous Asynchronous Inasynchronous operations, on the other hand, you can move to another task before the previous one finishes. This way, with asynchronous programming you’re able to deal with multiple requests simultaneously, Synchronous In synchronous operations tasks are performed one at a time and only when one is completed, the following is unblocked. In other words, you need to wait for a task to finish to move to the next one.
11 Async await inpractice Syntax: public async void/Task/Task<T> MethodAsync(param1, param2) { doSynchronousTask(); await doAsyncTask(); } • The async enables the await functionality in the method • You can not use await without using the async • A method can be declared as async without using await in the method body. It does work, but the just runs synchronously • Three types of return type void/Task/Task<T>
13 How is ituseful? For example, consider a web API call that reads data from a database. What happens when 1000 requests come in at the same time? Synchronous Way: • Need a separate thread for each request • ASP.NET has a maximum thread count (ex. 3000) • Max count will be reached very soon • Have to wait until some of the first requests are completed before they can even start Asynchronous Way: • As soon as the database request is made, the thread is freed while it waits for a response from the database. • During that waiting time, ASP.NET can use that thread to start processing a new request. • The result is that you need less threads to do the same amount of work • Increase the scalability
14.
14 Drawbacks • Code getsmore complex and harder to maintain. • There is increased memory allocation, as some objects have to stay alive longer while awaiting other code to be executed. • It can get hard to find bugs occurring in asynchronous tasks. • When we're writing an asynchronous piece of code, all our application code tends to become asynchronous.