public void DownloadItemsCompleted(object sender, DownloadCompletedEventArgs e) { try { ItemCache.SetCurrentSchedule(e.schedule); // success Debug.WriteLine("++++!!! items downloaded: " + e.schedule.Items.Aggregate("", (s, i) => s + i.Number + ", ")); // update fields (and GUI) UpdateItemFields(); } catch (Exception ex) { // failure Debug.WriteLine("Item download did not succeed. There may have been a problem with the data received"); } }
// this gets called by the updater thread when the operation is completed public void _DownloadCompleted(IAsyncResult result) { DownloadCompletedEventArgs e = new DownloadCompletedEventArgs(); try { HttpWebResponse response = (HttpWebResponse)((HttpWebRequest)result.AsyncState).EndGetResponse(result); string data = new StreamReader(response.GetResponseStream()).ReadToEnd(); // parse out the json into a string of xml and add to the event args try { Debug.WriteLine(data.Length + " characters received from API"); e.schedule = JsonConvert.DeserializeObject <Schedule>(data); // fire the DownloadCompleted event, in the UI thread if (Application.Current != null) { Application.Current.Dispatcher.BeginInvoke(new Action(() => { Debug.WriteLine("_DownloadCompleted: firing event!"); DownloadCompleted(this, e); })); } } catch (Exception ex) { Debug.WriteLine("Problem parsing item API result"); throw ex; } } catch { Debug.WriteLine("item download failed"); } }