Trying to learn about new async features in C #

I copied this example from here

I have seen many similar examples. Most of them say they use Async CTP. I am using Visual Studio 11 on Windows 8, although this is not applicable. As shown, the error says that TaskEx does not exist. I assume that I am missing the link, but I don’t know which one.

This page is http://users.zoominternet.net/~charleswatson/pic.png .

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static Random rnd = new Random(); static void Main(string[] args) { //Do some other heavy duty background task in this thread StartHotel(); Console.WriteLine("StartHotel called.."); Console.ReadLine(); } static void StartHotel() { Console.WriteLine("Starting Hotel.."); for (int i = 0; i < 10; i++) { string name = "Chef" + i; CookDish(name, "Dish" + i); Console.WriteLine("Asked {0} to start cooking at {1}", name, DateTime.Now.ToString()); } } static async void CookDish(string chefName, string dish) { //Induce a random delay int delay = rnd.Next(1000, 4000); //Cook is cooking - Task await TaskEx.Delay(delay); //Write the result - StuffAfterAwait Console.WriteLine("Chef {0} Finished at {1}", chefName, DateTime.Now.ToString()); } } } 
+7
source share
2 answers

In CTP, we were not able to add new functions to the Task type, so we did a pragmatic thing and just created a new TaskEx type. In the final version of this type will not be; these methods will only be on Task , as you expected.

+12
source

Replace TaskEx with Task . At the top of the .cs file you need:

 using System.Threading.Tasks; 

Most of the sample code I've seen relates to TaskEx, and the evaluated Mr. Lippert seems to indicate that there is an artifact of their development process. If you use the Developer preview, calls like Run, WhenAll, and Delay are already methods of the Task class, not TaskEx . Release tools should be the same.

+2
source

All Articles