From the course: Asynchronous Programming in C#

Creating and executing tasks - C# Tutorial

From the course: Asynchronous Programming in C#

Creating and executing tasks

- [Instructor] Task or the foundation for asynchronous programming in modern C#. Learning how to create and execute them is the first step to building fast and responsive apps. We'll start with some simple synchronous code, and then show you how you can quickly create a new task to run the code asynchronously. I've got a few lines of code already in place here, that will just help us keep track of when different lines are executing, and make it clear when they're running asynchronously. These Console.WriteLine calls will just let us know when the program starts running, and when it's ready to receive some user input. At the end, I've got a call to Console.Readline that could receive some input from the user, but will also make sure our console app doesn't shut down before we're done observing the output. I'm going to wrap our synchronous code inside a new function named ProcessData. I'll output another message to the console, so we know when the processing has begun. I'm not going to do any real work here, instead I'll just simulate some long-running data processing by calling Thread.Sleep. At the end I'll add one more Console.WriteLine call, just to let us know when the processing is completed. This will block the thread it's running on for a fixed number of milliseconds, I'm passing 3000 which will block the thread for three seconds. Okay, I'll now come down a few lines and call the function right after the program starts running. Let's run this and see what it looks like so far. I'll open the terminal built into Visual Studio Code and start the program with the command dotnet run. Remember, there's nothing asynchronous about the code at this point. The call to Thread.Sleep actually blocks the main thread. You can see that only after the fake processing is complete is the program ready for user input. I'll just hit enter to stop the program and then go back to the code. Let's now use a task to execute the process data function asynchronously. The simplest way to do that is to replace the current function with a call to Task.Run. The task will offload the function to a different thread and allow the code to resume execution immediately. I pass the name of the function to the run method. I'll go back to the terminal and run the code again. This time, you can see that right after the program starts it's ready for user input. The data processing is happening on another thread, and the messages about it show up later. That one simple call made our user interface much more responsive. Using the static run method on the task class is the easiest and most common way to create and start a task, but you can also use one of the task instructors to instantiate a new instance. I'll comment out the call to Task.Run and declare a new variable named DataTask. There are several constructor overloads on the task class, but I'll use the one that takes an action method as a parameter. I've passed it the process data function. So far, I've only created the new task. Using this technique, I have to start it manually, using the start method. Using this approach is a good idea if you need to conditionally start a task, I'll again go run this in the terminal, and show you that we get the same results. As you've seen, executing some otherwise synchronous work asynchronously, can be as easy as encapsulating the synchronous work in a task instance.

Contents