与异步和等待异步编程

2023-09-07 11:42:06 作者:青笙挽歌余梦初

我走过这个教程如何异步编程在C#和所遇到的我不知道如何解决一个错误。这里的链接:http://msdn.microsoft.com/en-us/library/hh191443.aspx而错误是:

 找不到的异步修改所需的所有类型。
你瞄准了错误的框架版本,或丢失的引用程序集?
 

我针对.NET 4.0框架,我不能确定所需的任何附加组件。

下面是code:

 公共异步任务<字符串> AccessTheWebAsync(1类1级,等级2等级2)
{
  // GetStringAsync返回任务<字符串取代。这意味着,当你等待
  //任务,你会得到一个List<字符串> (urlContents)。
  任务<字符串[]> listTask =的GetList(1级);

  //发送消息任务

  //你可以在这里做的工作,不依赖于从GetStringAsync字符串。
  // CompareService();

  //该计谋运营商暂停AccessTheWebAsync。
  //  -  AccessTheWebAsync无法继续下去,直到getStringTask完成。
  //  - 同时,控制返回到AccessTheWebAsync的调用者。
  //  - 控制恢复这里的时候getStringTask完成。
  //  - 该计谋运营商然后检索从getStringTask的字符串结果。
  字符串[] listContents =等待listTask;

  // return语句指定一个整数结果。
  //任何正在等待AccessTheWebAsync方法获取长度值。
  返回listContents;
}

公共任务<字符串[]>的GetList(1类1级)
{
    VAR taskArray =任务<字符串[]> .Factory.StartNew(()=> GenerateResults(1级));
    返回taskArray;
}
公共字符串[] GenerateResults(1类1级)
{
    字符串[]结果=新的字符串[2];
    结果[1] =;
    结果[2] =;
    返回结果;
}
 
如何优雅地实现 异步 编程

解决方案   

我针对.NET 4.0框架,我不能确定任何   需要额外的组件

它可以运行异步/计谋 code在.NET 4.0中没有isntalling .NET 4.5,having包括或异步CTP 引用 AsyncCtpLibrary.dll 。这是不可能的安装.NET 4.5或Windows XP的Visual Studio 2012和.NET 4.0没有.NET 4.5安装不同于.NET 4.0安装了.NET 4.5。 阅读为例,这个讨论:

在MSDN论坛:"If我的目标。NET 4.0,但运行的机器了.NET 4.5将.NET 4.0的WPF的错误依然存在吗?

我disadvise上机使用的NuGet没有.NET 4.5获取扩展.NET 4.0,因为它是真正把兼容包无论是对错误的.NET 4.5或.NET 4.0的.NET 4.5与.NET 4.0不兼容。 NET 4.5

但你的code有语法错误

您应该返回类型任务<字符串[]> ,而不是你的任务<字符串> ,在 AccessTheWebAsync()方法声明,也就是说,你应该写:

 公共异步任务<字符串[]> AccessTheWebAsync(1类1级,等级2等级2)()
 

而不是

 公共异步任务<字符串> AccessTheWebAsync(1类1级,等级2等级2)()
 

为了让这个方法返回类型的值的String []

 返回listContents; //在这里listContents被声明为String []类型
 

更新: 经过这一调整对我的true .NET 4.0(不包括.NET 4.5和VS2012)的Windows XP机器带有Async CTP

为什么我的答案downvoted?匿名...

显然,如果OP问这样的问题,他没有.NET 4.5安装。他将无法使用异步定位包参考异步用于.NET Framework 4,Silverlight的4和5,和Windows Phone 7.5和8 1.0.16无需安装VS2012,anв后者是根本不可能的WONDOWS XP,用的NuGet带来错误的包在VS2010不兼容,也不可能没有.NET 4.5 .NET 4.0使用安装

经过了很多次,在5月份的上下文

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;
}

解决方案

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:

on MSDN forum: "If I target .net 4.0 but run on a machine that has .net 4.5 will .net 4.0 WPF bugs still be there?"

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

相关推荐