Running tasks asynchronously
First, we will write a simple console application that needs to execute three actions.
Running multiple actions synchronously
Start Microsoft Visual Studio 2015. In Visual Studio, press Ctrl + Shift + N or go to File | New | Project….
In the New Project dialog, in the Installed Templates list, select Visual C#. In the center list, select Console Application, type the name as Ch12_Tasks, change the location to C:\Code
, type the solution name as Chapter12, and then click on OK.
Ensure that the following namespaces have been imported:
using System; using System.Threading; using System.Threading.Tasks; using System.Diagnostics; using static System.Console;
In the Program
class, add the following code. There are three methods that need to be executed: the first takes three seconds, the second takes two seconds, and the third takes one second. To simulate work, we can use the Thread
class to tell the current thread to go to sleep for a specified number of milliseconds...