Improve Performance on Your Windows Phone 7 Apps. Totally.Posted: March 27, 2011
Let’s get straight to it.
It’s all about the threads.
There are two main threads on WP7: UI thread, composition thread.
The UI thread handles user input, XAML parsing, drawing visuals and callbacks. A lot of work. So, don’t use it, it’s already got a lot on its plate.
The composition thread mainly aides the UI thread in handling graphics and animation, improving UI thread’s performance. In many cases the composition thread doesn’t kick in, so that’s no use either.
Enter, background threads.
These babies are amazing and the best way to enhance applications. And oh boy, it’s an enhancement. How does it work? Asynchronously. (Love that word.)
You can have the background threads can do all the dirty work using
BackgroundWorker and then when the dirty work is all ready to be presented, use either shared variables or my favourite,
Dispatcher.BeingInvoke to update the UI thread.
WebClient no more.
The web is everything and WP7’s way to the web is
WebClient. Unfortunately, it runs on the UI thread. You don’t want that, especially if the data you’re working with is large. MSDN recommends using HttpWebRequest. But why build a house from scratch when there’s Nuget? The package you’re looking for is Hammock. It’s what you want if you’re working with RESTful services. And once you have all the data call on to
Dispacther.BeginInvoke to send the data off to the UI. Beautiful stuff.
Let there be Opacity.
I’ll be truthful. I love
UIElement.Visibility. It’s just great hide stuff and unhide them whenever. Magic style. But,
UIElement.Opacity is just as amazing only faster. Black magic style. We all know black magic is better than magic. Here’s why.
UIElement.Opacity is set to zero, Silverlight will save the bitmap to memory but when
UIElement.Visibility changes, Silverlight redraws everything. Very tiresome. But to get this awesome boost you have to set
Media.BitmapCache, otherwise, it really is no magic and a lot of black-hole of performance hogs.
There’s another aspect to this though, if you’re in/visibling a lot of elements (by a lot I mean a lot), it’s best to measure the performance of both first and see which suits your needs. Also, you may consider selective in/visibling depending, again, on your needs.
Guess that’s all for now. I’ll wonder around to see if I can find other neat ways of enhancing performance for WP7 applications. Till then.