Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to learn about the new async features in c#

I copied this example from here

I have seen many similar examples. Most of them say they're using the Async CTP. I'm using Visual Studio 11 on Windows 8 though so that does not apply. As shown, the error says TaskEx doesn't exist. I assume I'm missing a reference but 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());
        }
    }
}
like image 588
Charles Watson Avatar asked Sep 27 '11 22:09

Charles Watson


People also ask

What is async C?

An async method runs synchronously until it reaches its first await expression, at which point the method is suspended until the awaited task is complete. In the meantime, control returns to the caller of the method, as the example in the next section shows.

What is the benefit of using async?

with async / await , you write less code and your code will be more maintainable than using the previous asynchronous programming methods such as using plain tasks. async / await is the newer replacement to BackgroundWorker , which has been used on windows forms desktop applications.

Why you shouldn't use async void?

Async void methods can wreak havoc if the caller isn't expecting them to be async. When the return type is Task, the caller knows it's dealing with a future operation; when the return type is void, the caller might assume the method is complete by the time it returns.

What is the purpose of async programming?

Asynchronous programming is a technique that enables your program to start a potentially long-running task and still be able to be responsive to other events while that task runs, rather than having to wait until that task has finished. Once that task has finished, your program is presented with the result.


1 Answers

In the CTP we were unable to add new features to the Task type so we did the pragmatic thing and just made a new TaskEx type. In the final release there will be no such type; those methods will just be on Task like you'd expect.

like image 159
Eric Lippert Avatar answered Oct 30 '22 03:10

Eric Lippert