Tôi đã sao chép ví dụ này từ hereĐang cố gắng tìm hiểu về các tính năng không đồng bộ mới trong C#
Tôi đã xem nhiều ví dụ tương tự. Hầu hết trong số họ nói rằng họ đang sử dụng Async CTP. Tôi đang sử dụng Visual Studio 11 trên Windows 8 mặc dù vậy mà không áp dụng. Như được hiển thị, lỗi cho biết TaskEx không tồn tại. Tôi cho rằng tôi thiếu một tham chiếu nhưng không biết cái nào.
Trang này là 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());
}
}
}
Đơn giản chỉ cần nhấp chuột phải-> mở. Hình ảnh được thu nhỏ qua css. – Femaref
@Femaref: Ah, hoàn hảo. Cảm ơn bạn –
Chỉ cần nhấp vào liên kết ví dụ –