How to deny updating/refreshing ListBox everytime in WP7?

A few things from a quick look at your code.

Up vote 0 down vote favorite share g+ share fb share tw.

I have a ListBox with ~40-150 items. When I click the Update button, my app checks for new news and messages on a background thread. If there are message or news updates, the app shows the notification.

Even if there are no new news or message items, my listbox refreshes and still does not respond for 1-10 seconds. How can I disable the update in this case? My code is clear and this threads works in a background.

I also have video of the problem. Private void UpdateNews(object sender, EventArgs e) { DispatcherTimer timer = new DispatcherTimer() { }; timer. Tick += (a, b) => { timer.Stop(); getData("post_upd"); }; timer.Start(); ProgBar.

IsIndeterminate = true; ProgBar. Visibility = System.Windows.Visibility. Visible; } public void getData(string filtr) { lock (this) { Debug.

WriteLine("News: Getting Data"); WebClient c = new WebClient(); WebClient be = new WebClient(); DateTime from = DateTime.Now. AddHours(-30); if (filtr == "post") { b. DownloadStringAsync(new Uri("https://api.vkontakte.

Ru/method/getServerTime? Access_token=" + ((App)Application. Current).

Access_token)); b. DownloadStringCompleted += new DownloadStringCompletedEventHandler(server_time_update); } if (filtr == "post_upd") { DispatcherTimer timer = new DispatcherTimer() { }; timer. Tick += (a, q) => { timer.Stop(); b.

DownloadStringAsync(new Uri("https://api.vkontakte. Ru/method/getServerTime? Access_token=" + ((App)Application.

Current). Access_token)); b. DownloadStringCompleted += new DownloadStringCompletedEventHandler(server_time_update); c.

DownloadStringAsync(new Uri("https://api.vkontakte. Ru/method/newsfeed. Get?

Start_time=" + ((App)Application. Current). Server_time + "&filters=post" + "&access_token=" + ((App)Application.

Current). Access_token)); c. DownloadStringCompleted += new DownloadStringCompletedEventHandler(posts_update); newslistBox.

PullSubtext = "? " + DateTime.Now. ToString("HH:mm:ss"); }; timer.Start(); } } private void after_server_time_update(string filtr) { WebClient c = new WebClient(); c.

DownloadStringAsync(new Uri("...?start_time=" + (((App)Application. Current). Server_time - 8600) + "&filters=" + filtr + "&access_token=" + ((App)Application.

Current). Access_token)); c. DownloadStringCompleted += new DownloadStringCompletedEventHandler(posts_downloaded); newslistBox.

PullSubtext = "? " + DateTime.Now. ToString("HH:mm:ss"); } private void posts_downloaded(object sender, DownloadStringCompletedEventArgs e) { if (e.

Error == null && (NetworkInterface. GetIsNetworkAvailable())) { lock (this) { Debug. WriteLine("News: Posts Downloaded online"); string json = e.

Result; var response = JObject. Parse(json); // Debug. WriteLine(response); string att_type = ""; list = new List(); prof_list = new List(); groups_list = new List(); Profiles p_item = null; Groups g_item = null; #region SWTICH switch (att_type) { case "photo":... break; case "graffiti":... list.

Add(item); break; case "audio":... }; list. Add(item); break; case "link":...}; list. Add(item); break; case "note": item = new Data {... list.

Add(item); break; case "post":... list. Add(item); break; case "poll": item = new Data {.... list. Add(item); break; case "video":.... list.

Add(item); break; } #endregion } this.newslistBox. ItemsSource = list; ProgBar. IsIndeterminate = false; ProgBar.

Visibility = Visibility. Collapsed; } } else { ToastPrompt toast = new ToastPrompt(); // Debug. WriteLine(who.ToString()); // Debug.

WriteLine(who.FirstOrDefault()4.ToString()); toast. TextOrientation = System.Windows.Controls.Orientation. Vertical; toast.

Title = "? "; toast. Message = "?

"; toast. MillisecondsUntilHidden = 7000; toast.Show(); } } private void posts_update(object sender, DownloadStringCompletedEventArgs e) { if (e. Error == null && (NetworkInterface.

GetIsNetworkAvailable())) { lock (this) { Debug. WriteLine("News: Posts Updated online"); string json = e. Result; var response = JObject.

Parse(json); if (response"response""items". HasValues) { // Debug. WriteLine(response); string att_type = ""; //list = new List(); prof_list = new List(); groups_list = new List(); Profiles p_item = null; Groups g_item = null; #region FOR I ITems for (int I = 0; I 0) { var x = from z in prof_list where z.

P_uid == (int)itemsi"source_id" select z; to_autorname = x.FirstOrDefault(). P_profile_image; to_auth = x.FirstOrDefault(). P_firstname + " " + x.FirstOrDefault().

P_lastname; } else { var x = from z in groups_list where z. G_uid == -(int)itemsi"source_id" select z; to_autorname = x.FirstOrDefault(). G_photo; to_auth = x.FirstOrDefault().

G_name; } #endregion #region SWTICH switch (att_type) { case "photo": item = new Data { attachment_type = "photo", image_src = itemsi"attachment""photo""src".ToString(), image_src_big = itemsi"attachment""photo""src_big".ToString(), text = itemsi"text".ToString(). Replace("", "\n"). Replace(""", "\""), likes_count = (int)itemsi"likes""count", pid_photo = (int)itemsi"attachment""photo""pid", owner_id = (int)itemsi"attachment""photo""owner_id", isLikeMe = Convert.

ToBoolean((int)itemsi"likes""user_likes"), uid = (int)itemsi"source_id", post_id = (int)itemsi"post_id", author_name = to_auth, image_uri = to_autorname, Date_Time = ((App)Application. Current). ConvertTimestamp(Convert.

ToDouble((int)itemsi"date")). ToString("dd MMM HH:mm:ss"), comm_count = (int)itemsi"comments""count", }; list. Insert(0, item); //list.

Add( item); break; case "graffiti": item = new Data { attachment_type = "graffiti", image_src = itemsi"attachment""graffiti""src".ToString(), image_src_big = itemsi"attachment""graffiti""src_big".ToString(), text = itemsi"text".ToString(). Replace("", "\n"). Replace(""", "\""), likes_count = (int)itemsi"likes""count", pid_photo = (int)itemsi"attachment""graffiti""gid", owner_id = (int)itemsi"attachment""graffiti""owner_id", isLikeMe = Convert.

ToBoolean((int)itemsi"likes""user_likes"), uid = (int)itemsi"source_id", post_id = (int)itemsi"post_id", author_name = to_auth, image_uri = to_autorname, Date_Time = ((App)Application. Current). ConvertTimestamp(Convert.

ToDouble((int)itemsi"date")). ToString("dd MMM HH:mm:ss"), comm_count = (int)itemsi"comments""count", }; list. Insert(0, item); // list.

Add(item); break; case "audio": item = new Data { attachment_type = "audio", audio_text = itemsi"attachment""audio""performer".ToString() + " - " + itemsi"attachment""audio""title".ToString(), audioUri = "", text = itemsi"text".ToString(). Replace("", "\n"). Replace(""", "\""), likes_count = (int)itemsi"likes""count", audioid = (int)itemsi"attachment""audio""aid", isLikeMe = Convert.

ToBoolean((int)itemsi"likes""user_likes"), uid = (int)itemsi"source_id", post_id = (int)itemsi"post_id", author_name = to_auth, image_uri = to_autorname, Date_Time = ((App)Application. Current). ConvertTimestamp(Convert.

ToDouble((int)itemsi"date")). ToString("dd MMM HH:mm:ss"), comm_count = (int)itemsi"comments""count", }; list. Insert(0, item); // list.

Add(item); break; case "link": item = new Data { attachment_type = "link", url = itemsi"attachment""link""url".ToString(), text = itemsi"text".ToString(). Replace("", "\n"). Replace(""", "\""), title = itemsi"attachment""link""title".ToString(), likes_count = (int)itemsi"likes""count", isLikeMe = Convert.

ToBoolean((int)itemsi"likes""user_likes"), uid = (int)itemsi"source_id", image_uri = to_autorname, author_name = to_auth, post_id = (int)itemsi"post_id", Date_Time = ((App)Application. Current). ConvertTimestamp(Convert.

ToDouble((int)itemsi"date")). ToString("dd MMM HH:mm:ss"), comm_count = (int)itemsi"comments""count", }; list. Insert(0, item); // list.

Add(item); break; case "note": item = new Data { attachment_type = "note", text = itemsi"text".ToString(). Replace("", "\n"). Replace(""", "\""), title = itemsi"attachment""note""title".ToString().

Replace(""", "\""), image_src = itemsi"attachment""link""image_src".ToString(), likes_count = (int)itemsi"likes""count", isLikeMe = Convert. ToBoolean((int)itemsi"likes""user_likes"), noteid = (int)itemsi"attachment""note""nid", owner_id = (int)itemsi"attachment""note""owner_id", uid = (int)itemsi"uid", image_uri = to_autorname, author_name = to_auth, post_id = (int)itemsi"post_id", Date_Time = ((App)Application. Current).

ConvertTimestamp(Convert. ToDouble((int)itemsi"date")). ToString("dd MMM HH:mm:ss"), comm_count = (int)itemsi"comments""count", }; list.

Insert(0, item); // list. Add(item); break; case "post": item = new Data { attachment_type = "post", text = itemsi"text".ToString(). Replace("", "\n").

Replace(""", "\""), likes_count = (int)itemsi"likes""count", isLikeMe = Convert. ToBoolean((int)itemsi"likes""user_likes"), uid = (int)itemsi"source_id", image_uri = to_autorname, author_name = to_auth, post_id = (int)itemsi"post_id", Date_Time = ((App)Application. Current).

ConvertTimestamp(Convert. ToDouble((int)itemsi"date")). ToString("dd MMM HH:mm:ss"), comm_count = (int)itemsi"comments""count", }; list.

Insert(0, item); // list. Add(item); break; case "poll": item = new Data { attachment_type = "poll", text = itemsi"text".ToString(). Replace("", "\n").

Replace(""", "\""), likes_count = (int)itemsi"likes""count", isLikeMe = Convert. ToBoolean((int)itemsi"likes""user_likes"), uid = (int)itemsi"source_id", image_uri = to_autorname, author_name = to_auth, poll_question = "? : " + itemsi"attachment""poll""question".ToString(), poll_id = (int)itemsi"attachment""poll""poll_id", post_id = (int)itemsi"post_id", Date_Time = ((App)Application.

Current). ConvertTimestamp(Convert. ToDouble((int)itemsi"date")).

ToString("dd MMM HH:mm:ss"), comm_count = (int)itemsi"comments""count", }; list. Insert(0, item); // list. Add(item); break; case "video": item = new Data { image_src = itemsi"attachment""video""image".ToString(), image_src_big = itemsi"attachment""video""image_big".ToString(), text = itemsi"text".ToString().

Replace("", "\n"). Replace(""", "\""), likes_count = (int)itemsi"likes""count", pid_photo = (int)itemsi"attachment""video""vid", owner_id = (int)itemsi"attachment""video""owner_id", attachment_type = "photo", isLikeMe = Convert. ToBoolean((int)itemsi"likes""user_likes"), uid = (int)itemsi"source_id", image_uri = to_autorname, author_name = to_auth, post_id = (int)itemsi"post_id", Date_Time = ((App)Application.

Current). ConvertTimestamp(Convert. ToDouble((int)itemsi"date")).

ToString("dd MMM HH:mm:ss"), comm_count = (int)itemsi"comments""count", }; list. Insert(0, item); // list. Add(item); break; } #endregion } //this.newslistBox.

ItemsSource = null; this.newslistBox. ItemsSource = list; Debug. WriteLine("Itemsspurce updated"); } else { ToastPrompt toast = new ToastPrompt(); //Debug.

WriteLine(who.ToString()); // Debug. WriteLine(who.FirstOrDefault()4.ToString()); toast. TextOrientation = System.Windows.Controls.Orientation.

Vertical; toast. Message = "? "; toast.

MillisecondsUntilHidden = 2000; toast.Show(); } ProgBar. IsIndeterminate = false; ProgBar. Visibility = Visibility.

Collapsed; }... c# silverlight windows-phone-7 listbox link|improve this question edited Jul 22 '11 at 0:31Michael Donohue6,36531124 asked Jul 20 '11 at 16:18wsevendays20810 81% accept rate.

If your application stops responding then its a threading issue. A video would be useless without code. – Ramhound Jul 20 '11 at 16:26 If you are binding the listbox to a data source which is getting modified by the thread.

Then that might be the reason for unresponsiveness of the listbox. – Amresh Kumar Jul 20 '11 at 17:01 1 Lets see some code. – Shawn Mclean Jul 20 '11 at 17:11 If there no new messages/news, the ItemsSource not updates.

Video will be in the near future. – wsevendays Jul 20 '11 at 18:48 youtube.com/… – wsevendays Jul 20 '11 at 19:15.

A few things from a quick look at your code: Don't use WebClient, as it returns on the UI thread which is probably the cause of your UI hanging issue. Instead use HttpWebRequest. Note that this behaviour changes in Mango NetworkInterface.

GetIsNetworkAvailable() can be slow. Do you really need to call it when you've got your response from the web request. Look at using a BackgroundWorker rather than creating a DispatcherTimer with no interval.

In that your requests are all to the same site, could htey not be combined (wrapped? ) into a single request? Probably the main issue: you are always replacing the list with what has been returned from the server regardless of whether it has changed or not.

You probably want to update the collection if it exists and there are changes with the data returned from the server. If sharing code to demonstrate a problem try and only include the minimum needed to recreate the issue. Give variables meaningful names.

It helps other people read (and hopefully understand) your code more easily. Seriousy, a 240+ line method?!? That's not a good way to create maintainable code.

Thanks! I have a very difficult response from the server and need to create the template for each of items in response. You helped me – wsevendays Jul 21 '11 at 11:11.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions