Ejemplo n.º 1
0
		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();
				}
			}
		}
Ejemplo n.º 2
0
        private AsyncEnumerator GetAsyncEnumerator()
        {
            AsyncEnumerator asyncEnumerator = new AsyncEnumerator();

            if (CancelTimeout > 0)
            {
                asyncEnumerator.SetCancelTimeout(CancelTimeout, null);
            }

            lock (_asyncEnumerators)
            {
                _asyncEnumerators.Add(asyncEnumerator);
            }

            return(asyncEnumerator);
        }
        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();
                    }
                }
            }
        }
Ejemplo n.º 4
0
        // Because SyncContext is set, all the iterator code runs on the GUI thread
        private IEnumerator<Int32> GetWebData(AsyncEnumerator ae, String[] uris)
        {
            ToggleStartAndCancelButtonState(false);
             m_lbResults.Items.Clear();

             // Auto-cancel after 5 seconds if the user desires
             if (m_chkAutoCancel.Checked)
            ae.SetCancelTimeout(5000, ae);

             // Issue several web requests (all in discard group 0) simultaneously
             foreach (String uri in uris) {
            WebRequest webRequest = WebRequest.Create(uri);

            // If the AsyncEnumerator is canceled, DiscardWebRequest cleans-up
            // any outstanding operations as they complete in the future
            webRequest.BeginGetResponse(ae.EndVoid(0, DiscardWebRequest), webRequest);
             }

             yield return uris.Length;  // Process the completed web requests after all complete

             String resultStatus; // Ultimate result of processing shown to user

             // Check if iterator was canceled
             Object cancelValue;
             if (ae.IsCanceled(out cancelValue)) {
            // Tell the AE to auto-cleanup any operations issued as part of discard group 0
            ae.DiscardGroup(0);
            // Note: In this example calling DiscardGroup above is not mandatory
            // because the whole iterator is stopping execution; causing all
            // discard groups to be discarded automatically.

            resultStatus = (cancelValue == ae) ? "Timeout" : "User canceled";
            goto Complete;
             }

             // Iterator wasn't canceled, process all the completed operations
             for (Int32 n = 0; n < uris.Length; n++) {
            // Grab the result of a completed web request
            IAsyncResult result = ae.DequeueAsyncResult();

            // Get the WebRequest object used to initate the request
            // (see BeginGetResponse's last argument above)
            WebRequest webRequest = (WebRequest)result.AsyncState;

            // Build the String showing the result of this completed web request
            String s = "URI=" + webRequest.RequestUri + ", ";
            try {
               using (WebResponse webResponse = webRequest.EndGetResponse(result)) {
                  s += "ContentLength=" + webResponse.ContentLength;
               }
            }
            catch (WebException e) {
               s += "Error=" + e.Message;
            }
            m_lbResults.Items.Add(s);  // Add result of operation to listbox
             }
             resultStatus = "All operations completed.";

              Complete:
             // All operations have completed or cancelation occurred, tell user
             MessageBox.Show(this, resultStatus);

             // Reset everything so that the user can start over if they desire
             m_ae = null;   // Reset since we're done
             ToggleStartAndCancelButtonState(true);
        }