我一直在使用AsyncEnumerator类来帮助清理我的异步代码。问题是收益率返回后的代码永远不会触发。 UI调用一个对象,该对象在单击按钮时调用HttpWorker对象。无论点击多少按钮,代码都不会超过收益率。
public void ReadFromUrl(string Url)
{
AsyncEnumerator asyncEnum = new AsyncEnumerator();
asyncEnum.BeginExecute(GetData(asyncEnum,Url), asyncEnum.EndExecute);
}
private IEnumerator<Int32> GetData(AsyncEnumerator AsyncEnum, String Url)
{
url = Url;
request = (HttpWebRequest)WebRequest.Create(url);
request.CookieContainer = new CookieContainer();
request.AllowAutoRedirect = true;
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6";
request.BeginGetResponse(AsyncEnum.End(), null);
AsyncEnum.SyncContext = null;
yield return 1;
response = (HttpWebResponse)request.EndGetResponse(AsyncEnum.DequeueAsyncResult());
if (response.StatusCode == HttpStatusCode.OK)
{
// Create the stream, encoder and reader.
Stream responseStream = response.GetResponseStream();
Encoding streamEncoder = Encoding.UTF8;
StreamReader responseReader = new StreamReader(responseStream, streamEncoder);
responseAsString = responseReader.ReadToEnd();
}
else
{
throw new Exception(String.Format("Response Not Valid {0}", response.StatusCode));
}
}
答案 0 :(得分:0)
除了我的调试使用没有错!