// Starts the Info-Center after downloading the data private void loadWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Cancelled) { Stop(); } else if (e.Error != null) { Stop(); LogError("Error at loading data for Info-Center.", e.Error); } else if (e.Result is WebData) { Stop(false); mPageIndex = 0; var webData = e.Result as WebData; mInfoCenterData = webData.InfoCenterData; mUrl2Image = webData.Url2Image; mWeatherData = webData.WeatherData; if (mInfoCenterData != null && mInfoCenterData.Pages.Count > 0) { ShowPage(); // Start the timer immediately mTimer.Start(); OnStatusChanged(Status.Running); } } }
/// <summary> /// Stops the InfoCenter and clears the data. /// </summary> public void Stop(bool sendEvent = true) { mLoadDataWorker.CancelAsync(); mTimer.Stop(); mInfoCenterData = null; mWeatherData = null; mUrl2Image.Clear(); if (sendEvent) { OnStatusChanged(Status.Stopped); } }
// Download data from web private void loadWorker_DoWork(object sender, DoWorkEventArgs e) { var worker = (BackgroundWorker)sender; var data = new WebData(); using (var reader = XmlReader.Create(AicSettings.Global.InfoCenterDataUrl)) { data.InfoCenterData = InfoCenterData.Deserialize(reader); } if (worker.CancellationPending) { e.Cancel = true; return; } if (AicSettings.Global.WeatherEnabled) { data.WeatherData = ReadWeatherData(); } if (worker.CancellationPending) { e.Cancel = true; return; } data.Url2Image = new Dictionary <string, ImageSource>(); foreach (var page in data.InfoCenterData.Pages) { if (!string.IsNullOrWhiteSpace(page.ImageUrl)) { //BitmapImage bitmapImage = new BitmapImage(new Uri(page.ImageUrl)); try { BitmapImage bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = Network.DownloadStreamData(page.ImageUrl); bitmapImage.EndInit(); bitmapImage.Freeze(); // Freezing is very important because the data is given to another thread data.Url2Image[page.ImageUrl] = bitmapImage; } catch { data.Url2Image[page.ImageUrl] = null; } if (worker.CancellationPending) { e.Cancel = true; return; } } } // Remove pages where the image could not be downloaded data.InfoCenterData.Pages.RemoveAll(x => !string.IsNullOrWhiteSpace(x.ImageUrl) && data.Url2Image[x.ImageUrl] == null); if (worker.CancellationPending) { e.Cancel = true; return; } e.Result = data; }