Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
969 views
in Technique[技术] by (71.8m points)

c# - Asynchronous Programming with Async and Await

I'm walking through this tutorial on how to program asynchronously in c# and have come across an error I'm not sure how to resolve. Here's the link: http://msdn.microsoft.com/en-us/library/hh191443.aspx and the error is:

Cannot find all types required by the 'async' modifier.  
Are you targeting the wrong framework version, or missing a reference to an assembly?   

I am targeting the .NET 4.0 framework and am unsure as to any additional assemblies required.

Here is the code:

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I am targeting the .NET 4.0 framework and am unsure as to any additional assemblies required

It is possible to run async/await code in .NET 4.0 without isntalling .NET 4.5, having included or referenced AsyncCtpLibrary.dll from Async CTP. It is impossible to install .NET 4.5 or Visual Studio 2012 on Windows XP and .NET 4.0 without .NET 4.5 installed is different from .NET 4.0 with installed .NET 4.5.
Read, for example, this discussion:

I disadvise to use Nuget on machine without .NET 4.5 for getting extensions for .NET 4.0 as it is really bringing compatible packs either for wrong .NET 4.5 or for .NET 4.0 from .NET 4.5 incompatible with .NET 4.0 without .NET 4.5

But your code has a syntax error

You should have return type Task<string[]>, instead of your Task<string>, in AccessTheWebAsync() method declaration, i.e. you should have written:

public async Task<string[]> AccessTheWebAsync(Class1 class1, Class2 class2)()  

instead of

public async Task<string> AccessTheWebAsync(Class1 class1, Class2 class2)()

in order for this method to return values of type string[]:

return listContents;//where listContents is declared as string[] type   

Update:
Checked that OP's code run after this correction on my true .NET 4.0 (without .NET 4.5 and VS2012) Windows XP machine with Async CTP

Why was my answer downvoted? anonymously...

It is obvious that if OP asks such question that he does not have .NET 4.5 installed. He will not be able to use Async Targeting Pack referencing "Async for .NET Framework 4, Silverlight 4 and 5, and Windows Phone 7.5 and 8 1.0.16" without installing VS2012, anв the latter is simply impossible on Wondows XP, with Nuget bring wrong packs in VS2010 incompatible and impossible to use on .NET 4.0 without .NET 4.5 installed

Checked it many times, in may contexts


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...