About the Author xi
About the Technical Reviewer xiii
Acknowledgments xv
Introduction xvii
Chapter 1: Asynchronous Programming and Tasks 1
Chapter 2: Task Creation and Execution 13
Chapter 3: Continuation and Nested Tasks 41
Chapter 4: Exception Handling 73
Chapter 5: Managing Cancellations 99
Chapter 6: Bonus 133
Appendix A: What’s Next? 157
Appendix B: Other Books by the Author 159
Index 161
At a high level, to understand asynchronous programming, you need to be familiar with task programming, efficient use of the async and await keywords, and a few more important topics. However, task programming is the first major step towards modern-day asynchronous programming. This book tries to simplify the topic with simple examples, Q&A sessions, and exercises.
The book starts with an introduction to asynchronous programming and covers task creation and execution. Next, you will learn how to work with continuing and nested tasks. Next, it demonstrates exception handling with different scenarios. Towards the end, you will understand how to manage task cancellations through examples and case studies. After reading this book, you can write efficient codes for multithreaded, asynchronous, and parallel development in C#.
With the availability of multicore computers, asynchronous programming and parallel programming are becoming increasingly important. Why not? It is essential for building highly responsive software. This is why playing with threads in a multithreaded environment is inevitable. Undoubtedly, it is hard, but in earlier days, it was harder. To simplify the overall coding experience, starting from the .NET Framework 4.0, Microsoft introduced Task Parallel Library (TPL) which was based on the concept of tasks. Later, in C#5, we saw the revolutionary introduction of the async and await keywords. Using them, we started passing the heavy work(s) to the compiler. However, you need to remember that a typical async method normally returns a task (in programming terms, a Task or a Task TResult). So, there is no wonder that task programming became the modern-day foundation for asynchronous programming.