From the course: Asynchronous Programming in C#
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Waiting for tasks to complete - C# Tutorial
From the course: Asynchronous Programming in C#
Waiting for tasks to complete
- [Instructor] Once you've created and started a task, your next logical question is probably: How do I know when it's done and how do I access any result it produced? There are a number of ways to answer both of those questions and I'll gradually show you several of them. One way to know when a task is done is just to wait on it. The static run method I've already shown you returns a task instance. Task objects have a method on them named Wait. Let's imagine that before this call to Console.WriteLine, I call that method on my dataTask object. This probably looks like a natural thing to do. Unfortunately, calling the Wait method is a blocking operation. Our application won't report that it's ready for user input until the asynchronous work is complete. Calling Wait like this immediately after starting a task negates the asynchronous benefits of even using a task and effectively makes this code synchronous. We'll see better…