private IEnumerator<Int32> GetImages(AsyncEnumerator ae) { ae.ThrowOnMissingDiscardGroup(true); // Request all the images asynchronously WebRequest[] requests = new WebRequest[] { WebRequest.Create(_imageUris[0]), WebRequest.Create(m_chkSimFailure.Checked ? _imageUris[2] : _imageUris[1]) }; for(Int32 n = 0; n < requests.Length; n++) requests[n].BeginGetResponse( ae.EndVoid(0, asyncResult => { requests[(Int32) asyncResult.AsyncState].EndGetResponse(asyncResult).Close(); }), n); // Set timeout if specified. Int32 timeout; if(Int32.TryParse(m_txtServerTime.Text, out timeout)) ae.SetCancelTimeout(timeout, null); // WaitAsync for all operations to complete (or timeout) yield return requests.Length; if(ae.IsCanceled()) { m_lblPic1.Text = "Server couldn't process the request in the specified time."; yield break; } for(Int32 n = 0; n < requests.Length; n++) { IAsyncResult result = ae.DequeueAsyncResult(); Int32 reqNum = (Int32) result.AsyncState; Label lbl = (reqNum == 0) ? m_lblPic1 : m_lblPic2; WebRequest request = requests[reqNum]; WebResponse response = null; try { response = request.EndGetResponse(result); lbl.Text = String.Format("Image at {0} is {1:N0} bytes", response.ResponseUri, response.ContentLength); } catch(WebException e) { lbl.Text = String.Format("Error obtaining image at {0}: {1}", request.RequestUri, e.Message); } finally { if(response != null) response.Close(); } } }
private IEnumerator<Int32> DownloadImages(AsyncEnumerator ae) { ae.ThrowOnMissingDiscardGroup(true); GetImages.IsEnabled = false; Cancel.IsEnabled = true; WebRequest[] requests = new WebRequest[] { WebRequest.Create(UriHelper.ConvertRelativeUriStringToAbsolute("Images/Wintellect.jpg")), WebRequest.Create(UriHelper.ConvertRelativeUriStringToAbsolute("Images/JeffreyRichter.jpg")), }; for (Int32 requestNum = 0; requestNum < requests.Length; requestNum++) { requests[requestNum].BeginGetResponse( ae.EndVoid(0, asyncResult => { requests[requestNum].EndGetResponse(asyncResult).Close(); }), requestNum); } for (Int32 resultNum = 0; resultNum < requests.Length; resultNum++) { yield return 1; if (ae.IsCanceled()) break; IAsyncResult asyncResult = ae.DequeueAsyncResult(); Int32 index = (Int32)asyncResult.AsyncState; try { using (WebResponse response = requests[index].EndGetResponse(asyncResult)) { using (Stream stream = response.GetResponseStream()) { BitmapImage bitmapImage = new BitmapImage(); bitmapImage.SetSource(stream); m_images[index].Source = bitmapImage; } } } catch (WebException e) { m_textboxes[index].Text = "Failed: " + e.Message; } } GetImages.IsEnabled = true; Cancel.IsEnabled = false; }
private IEnumerator <Int32> GetImages(AsyncEnumerator ae) { ae.ThrowOnMissingDiscardGroup(true); // Request all the images asynchronously WebRequest[] requests = new WebRequest[] { WebRequest.Create(_imageUris[0]), WebRequest.Create(m_chkSimFailure.Checked ? _imageUris[2] : _imageUris[1]) }; for (Int32 n = 0; n < requests.Length; n++) { requests[n].BeginGetResponse( ae.EndVoid(0, asyncResult => { requests[(Int32)asyncResult.AsyncState].EndGetResponse(asyncResult).Close(); }), n); } // Set timeout if specified. Int32 timeout; if (Int32.TryParse(m_txtServerTime.Text, out timeout)) { ae.SetCancelTimeout(timeout, null); } // WaitAsync for all operations to complete (or timeout) yield return(requests.Length); if (ae.IsCanceled()) { m_lblPic1.Text = "Server couldn't process the request in the specified time."; yield break; } for (Int32 n = 0; n < requests.Length; n++) { IAsyncResult result = ae.DequeueAsyncResult(); Int32 reqNum = (Int32)result.AsyncState; Label lbl = (reqNum == 0) ? m_lblPic1 : m_lblPic2; WebRequest request = requests[reqNum]; WebResponse response = null; try { response = request.EndGetResponse(result); lbl.Text = String.Format("Image at {0} is {1:N0} bytes", response.ResponseUri, response.ContentLength); } catch (WebException e) { lbl.Text = String.Format("Error obtaining image at {0}: {1}", request.RequestUri, e.Message); } finally { if (response != null) { response.Close(); } } } }