Tôi đang xem qua hướng dẫn này về cách lập trình không đồng bộ trong C# và đã gặp lỗi tôi không chắc chắn cách giải quyết. Đây là liên kết: http://msdn.microsoft.com/en-us/library/hh191443.aspx và lỗi này là:Lập trình không đồng bộ với Async và Await
Cannot find all types required by the 'async' modifier.
Are you targeting the wrong framework version, or missing a reference to an assembly?
Tôi đang nhắm mục tiêu .NET framework 4.0 và không chắc chắn như với bất kỳ hội bổ sung cần thiết.
Đây là mã:
public async Task<string> AccessTheWebAsync(Class1 class1, Class2 class2)
{
// GetStringAsync returns a Task<string>. That means that when you await the
// task you'll get a List<string> (urlContents).
Task<string[]> listTask = GetList(class1);
// send message task
// You can do work here that doesn't rely on the string from GetStringAsync.
//CompareService();
// The await operator suspends AccessTheWebAsync.
// - AccessTheWebAsync can't continue until getStringTask is complete.
// - Meanwhile, control returns to the caller of AccessTheWebAsync.
// - Control resumes here when getStringTask is complete.
// - The await operator then retrieves the string result from getStringTask.
string[] listContents = await listTask;
// The return statement specifies an integer result.
// Any methods that are awaiting AccessTheWebAsync retrieve the length value.
return listContents;
}
public Task<string[]> GetList(Class1 class1)
{
var taskArray = Task<string[]>.Factory.StartNew(() => GenerateResults(class1));
return taskArray;
}
public string[] GenerateResults(Class1 class1)
{
string[] results = new string[2];
results[1] = "";
results[2] = "";
return results;
}
async - chờ đợi là từ .NET framework 4.5, không 4 –
@CuongLe, 'async/await' là từ' C# -5.0' nhưng tôi sử dụng nó trong .NET 4.0 đã được cài đặt 'Async CTP' b/c Tôi không thể cài đặt .NET-4.5 (hoặc VS2012) trên Windows XP (VS2010). Hãy nghĩ về nó như là phần mở rộng cho đúng .NET-4.0, không phải là phiên bản .NET-4.5 chỉ –