public TivoContainer EndExecute(IAsyncResult asyncResult) { WebClientAsyncResult webClientAsyncResult = asyncResult as WebClientAsyncResult; if (webClientAsyncResult == null) { throw new ArgumentException("IAsyncResult did not come from BeginQueryContainer", "asyncResult"); } if (webClientAsyncResult.Error != null) { throw webClientAsyncResult.Error; } return((TivoContainer)webClientAsyncResult.Result); }
public IAsyncResult BeginExecute(AsyncCallback asyncCallback, object asyncState) { if (_connection.State != TivoConnectionState.Open) { throw new InvalidOperationException(); } Uri uri; PrepareQueryContainer(_connection.WebClient, out uri); _connection.WebClient.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted); WebClientAsyncResult asyncResult = new WebClientAsyncResult(asyncCallback, asyncState); _connection.WebClient.OpenReadAsync(uri, asyncResult); return(asyncResult); }
void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) { // unregister so query and connection can be reused. _connection.WebClient.OpenReadCompleted -= new OpenReadCompletedEventHandler(client_OpenReadCompleted); // return object WebClientAsyncResult asyncResult = (WebClientAsyncResult)e.UserState; try { using (var stream = e.Result) using (var reader = new System.IO.StreamReader(stream)) { var doc = System.Xml.Linq.XDocument.Load(reader); asyncResult.Result = (TivoContainer)doc; } } catch (Exception ex) { asyncResult.Error = ex; } asyncResult.AsyncCallback(asyncResult); // TODO: what to do with errors and cancelled? }