private async Task AddItems(string s) { bb = new List <Book>(); var task = Task.Factory.StartNew(() => { try { var webClient = new WebClient(); string result = webClient.DownloadString(MakeQuery2(s)); API_Data data = JsonConvert.DeserializeObject <API_Data>(result); foreach (Books b in data.Books) { if (b.Book.Author == null) { b.Book.Author = "Uknown author"; } if (b.Book.Year == "0") { b.Book.Year = "Uknown"; } b.Book.Name = b.Book.Name.Trim(); if (b.Book.Name.Length > 50) { b.Book.Name = b.Book.Name.Substring(0, 50) + "..."; } if (b.Book.Author.Length > 30) { b.Book.Author = b.Book.Author.Substring(0, 30) + "..."; } if (b.Book.Annotation.Length > 100) { b.Book.Annotation = b.Book.Annotation.Substring(0, 100) + "..."; } Result1(b.Book); } } catch (Exception ex) { MessageBox.Show("An error occured: " + ex.Message); MessageBox.Show("Возможно у вас отсутствует Интернет соединение!"); } }); await task; }
private void searchButton_Click(object sender, RoutedEventArgs e) { var s = searchBox.Text.Trim(); if (s == "") { return; } searchBox.Text = ""; listBox1.Items.Clear(); try { bb = new List <Book>(); var webClient = new WebClient(); string result = webClient.DownloadString(MakeQuery1(s)); API_Data data = JsonConvert.DeserializeObject <API_Data>(result); foreach (Books b in data.Books) { if (b.Book.Author == null) { b.Book.Author = "Uknown author"; } if (b.Book.Year == "0") { b.Book.Year = "Uknown"; } if (b.Book.Name.Length > 50) { b.Book.Name = b.Book.Name.Substring(0, 50) + "..."; } Result1(b.Book); } dataGrid2.ItemsSource = bb; } catch (Exception ex) { MessageBox.Show("An error occured: " + ex.Message); } }