Why Asynchronous programming?
Because by default code works synchronously which runs everything on a same thread and can block the main thread.
Asynchronous programming however runs the divided and runs the code on different threads and not blocking the main thread.
In Asynchronous programming, async and wait keywords are used together.
Asynchronous programming in .Net has evolved over time, for example from Threading and Background Worker to Task Parallel Library and Async and Await (pauses execution of a particular method while fetching the result ).
Where can we use asynchronous programming?
For communication – I/O operations:-
How to convert synchrnous code to asynchronous?
- Add async keyword to methods
- Add await keyword inside method to
- Avoid async method with void return type unless working with event handler.
- Change void to Task