Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# silverlight/wp7:HTTPwebrequest BeginGetResponse lambda表达式工作不正常_C#_Silverlight_Windows Phone 7 - Fatal编程技术网

C# silverlight/wp7:HTTPwebrequest BeginGetResponse lambda表达式工作不正常

C# silverlight/wp7:HTTPwebrequest BeginGetResponse lambda表达式工作不正常,c#,silverlight,windows-phone-7,C#,Silverlight,Windows Phone 7,我正在使用lambda表达式在一个循环中调用HTTPwebrequest BeginGetResponse(这里索引在循环中每次递增) 尝试使用以下两种方法,但是当调用OnHTMLFetchComplete时,我只得到最终索引值,而不是中间索引值 备选案文1: HttpWebRequest itemHtmlRequest = (HttpWebRequest)HttpWebRequest.Create(new Uri(itemDetail.Links)); itemHtmlRequest.

我正在使用lambda表达式在一个循环中调用HTTPwebrequest BeginGetResponse(这里索引在循环中每次递增)

尝试使用以下两种方法,但是当调用OnHTMLFetchComplete时,我只得到最终索引值,而不是中间索引值

备选案文1:

  HttpWebRequest itemHtmlRequest = (HttpWebRequest)HttpWebRequest.Create(new Uri(itemDetail.Links));
  itemHtmlRequest.BeginGetResponse(result => OnHTMLFetchComplete(result, index, itemHtmlRequest),null);
备选案文2:

  HttpWebRequest itemHtmlRequest = (HttpWebRequest)HttpWebRequest.Create(new Uri(itemDetail.Links));

  itemHtmlRequest.BeginGetResponse(new AsyncCallback(
      result => OnHTMLFetchComplete(result, index, itemHtmlRequest)), null);

如果没有看到整个代码,我猜在任何异步代码收到任何HTTP响应之前,外部循环迭代已经完成。

如果没有看到整个代码,我的猜测是,在任何异步代码收到任何HTTP响应之前,外部循环迭代已经完成。

这是捕获循环变量的常见问题。lambda表达式捕获的是
索引
变量,而不是其值。不过,这是一个简单的解决方案:

for (int index = 0; index < ...; index++)
{
    int indexCopy = index;
    Uri uri = ...;
    HttpWebRequest itemHtmlRequest = WebRequest.CreateHttp(uri);
    itemHtmlRequest.BeginGetResponse(
        result => OnHTMLFetchComplete(result, indexCopy, itemHtmlRequest),null);
}
for(int index=0;index<…;index++)
{
int indexCopy=索引;
Uri=。。。;
HttpWebRequest itemHtmlRequest=WebRequest.CreateHttp(uri);
itemHtmlRequest.BeginGetResponse(
结果=>OnHTMLFetchComplete(结果、索引复制、项HtmlRequest),null;
}
这里您捕获的是
indexCopy
而不是
index
——但是,虽然只有一个
index
变量,但在循环的每次迭代中都有一个新的
indexCopy
变量。
index
的值会随着时间的推移而变化,而
indexCopy
的值则不会变化,所以您没事

埃里克·利珀特(Eric Lippert)有两篇关于这一点的博文:


(注意:有很多问题的答案是相似的。但是,所有的实际问题都是不同的。我个人认为回答每个不同的问题是值得的,希望以后能更容易地找到类似的问题。)

这是捕获循环变量的常见问题。lambda表达式捕获的是
索引
变量,而不是其值。不过,这是一个简单的解决方案:

for (int index = 0; index < ...; index++)
{
    int indexCopy = index;
    Uri uri = ...;
    HttpWebRequest itemHtmlRequest = WebRequest.CreateHttp(uri);
    itemHtmlRequest.BeginGetResponse(
        result => OnHTMLFetchComplete(result, indexCopy, itemHtmlRequest),null);
}
for(int index=0;index<…;index++)
{
int indexCopy=索引;
Uri=。。。;
HttpWebRequest itemHtmlRequest=WebRequest.CreateHttp(uri);
itemHtmlRequest.BeginGetResponse(
结果=>OnHTMLFetchComplete(结果、索引复制、项HtmlRequest),null;
}
这里您捕获的是
indexCopy
而不是
index
——但是,虽然只有一个
index
变量,但在循环的每次迭代中都有一个新的
indexCopy
变量。
index
的值会随着时间的推移而变化,而
indexCopy
的值则不会变化,所以您没事

埃里克·利珀特(Eric Lippert)有两篇关于这一点的博文:


(注意:有很多问题都有相似的答案。但是,所有的实际问题都是不同的。我个人认为回答每个不同的问题是值得的,希望以后能更容易地找到相似的问题。)

这似乎也是WP7环境中第一次出现这种情况。有些人会专门在标签上搜索类似的问题。非常感谢!哇,这么简单的解决方法,我花了好几个小时试图解决这个问题。知识的力量。:-)这似乎也是第一次在WP7环境中出现。有些人会专门在标签上搜索类似的问题。非常感谢!哇,这么简单的解决方法,我花了好几个小时试图解决这个问题。知识的力量。:-)