Example #1
0
        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);
        }
Example #2
0
        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);
        }
Example #3
0
        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?
        }