public IAsyncResult BeginGetShippingRatesAsync(decimal weight, string originZipCode, string destinationZipCode, AsyncCallback callback, object state) { // create object that will store results AsyncResult<ShippingRate[]> asyncResult = new AsyncResult<ShippingRate[]>(callback, state); ShippingRatesRequest request = new ShippingRatesRequest(asyncResult, 3); // launch asynchronous requests, which will complete in parallel WebRequest fedExRequest = CreateFedExRequest(weight, originZipCode, destinationZipCode); fedExRequest.BeginGetResponse(ar => FedExCallback(request, fedExRequest, ar), null); WebRequest upsRequest = CreateUpsRequest(weight, originZipCode, destinationZipCode); upsRequest.BeginGetResponse(ar => UpsCallback(request, upsRequest, ar), null); WebRequest uspsRequest = CreateUspsRequest(weight, originZipCode, destinationZipCode); uspsRequest.BeginGetResponse(ar => UspsCallback(request, uspsRequest, ar), null); // return IAsyncResult implementation to client return asyncResult; }
public IAsyncResult BeginGetResponse(AsyncCallback callback, object state) { AsyncResult<WebResponse> asyncResult = new AsyncResult<WebResponse>(callback, state); ThreadPool.QueueUserWorkItem(_ => asyncResult.Finish(GetResponse(), false)); return asyncResult; }
public ShippingRatesRequest(AsyncResult<ShippingRate[]> asyncResult, int outstandingRequests) { m_lock = new object(); m_rates = new List<ShippingRate>(); m_asyncResult = asyncResult; m_outstandingRequests = outstandingRequests; }
public IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state) { AsyncResult<Stream> asyncResult = new AsyncResult<Stream>(callback, state); ThreadPool.QueueUserWorkItem(_ => asyncResult.Finish(new MemoryStream(), false)); return asyncResult; }