Handling tasks in asynchronous programming
Task-Based Asynchronous Pattern (TAP) is now the recommended method to create asynchronous code. It executes asynchronously on a thread from the thread pool and does not execute synchronously on the main thread of your application. It allows us to check the task's state by calling the Status
property.
Getting ready
We will create a task to read a very large text file. This will be accomplished using an asynchronous Task
.
How to do it…
- Create a large text file (we called ours
taskFile.txt
) and place it in yourC:\temp
folder: - In the
AsyncDemo
class, create a method calledReadBigFile()
that returns aTask<TResult> ...
Get C# Programming Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.