This article aims to collect the most important performance challenges I’ve come across when working on various WP7 projects – the most well known being Visiblox Charts for Windows Phone 7 and Cocktail Flow (On a note, I gave a talk on this topic at the London Windows Phone 7 user group – the slides from that event can be viewed from here).
Networking Causing UI Lags
The most common class to use for networking tasks is the WebClient class. It’s the simplest networking class to use, it takes 3 lines to grab a piece of web content:
var client = new WebClient(); client.DownloadStringCompleted += (s, ev) => { responseTextBlock.Text = ev.Result; }; client.DownloadStringAsync(new Uri("http://www.sherdog.com/rss/news.xml"))
Unfortunately there’s a serious issue that’s not mentioned in the documentation of this class – it runs on the UI thread most of the time making the UI unresponsive at times.
The workaround to this issue is to not use WebClient, but use the HttpWebRequest class (which is actually used by WebClient as well). HttpWebRequest has a bit less straightforward API than WebClient and – since it doesn’t run on the UI thread – it’s the caller’s responsibility to marshall back to the UI thread:
var request = (HttpWebRequest)WebRequest.Create(new Uri("http://www.sherdog.com/rss/news.xml")); request.BeginGetResponse(r => { var httpRequest = (HttpWebRequest)r.AsyncState; var httpResponse = (HttpWebResponse)httpRequest.EndGetResponse(r); using (var reader = new StreamReader(httpResponse.GetResponseStream())) { var response = reader.ReadToEnd(); Deployment.Current.Dispatcher.BeginInvoke(new Action(() => { responseTextBlock.Text = response; })); } }, request);

