Email the editor
Facebook
Twitter
YouTube
Buy a phone
Find apps + games
Get help + tips
Another big milestone for Windows Phone
Marketplace arrives in 13 new countries
Transferring your stuff to a new phone
6 tricks for personalizing your phone
Apps for baseball fans
Windows Phone Minute (tips)
Hot Apps
Nokia Conversations
Windows Phone Developer Blog
This is part three of my Widget Anatomy series which which will explain the ins and outs of the Widget Framework that is shipping as part of Windows Mobile 6.5. In this installment I will discuss how to squeeze the last drop of performance out of the Widget framework to ensure all our Widgets are interactive, alive and fun to use while being good mobile citizens and not draining the user’s battery dry in the process.
JavaScript is a cool language. It’s simple to learn, use, and understand but it really can get us into a lot of trouble if not used wisely. this is true on all platforms but we must pay special attention to its implications while developing for a mobile platform.
So, just because we are among friends here I’ll share a little secret that showcases the biggest challenge to creating responsive Widgets. Here it goes… are you ready?… On Windows Mobile 6.5, JavaScript execution blocks the UI. This means that, whenever there is a script running nothing else can be processed in the widget which includes menu commands and keyboard or touch input processing. Also note that, during script execution the Widget can’t redraw – in other words, any screen updates done during the execution of the script will not be reflected on the screen until after the script finishes its execution.
I know that this sounds like a big obstacle to overcome but it is really not that bad.
On very interesting aspect of performance is that, in most cases, perception is reality. In a mobile application world, this means that for every user action there is a immediate response from the application that assures the user that their input was acknowledged and is being processed.
The best way to accomplish this on widgets is to minimize the amount of code that is executed on the event handler and defer all actions that are not related to updating the UI to give feedback to the user after a small timeout.
1: function onCoolActionClick() {
2: document.getElementById('waitCursorLayer').style.display = "block";
3: window.setTimeout("doCoolStuff()", 50);
4: }
5:
6: function doCoolStuff() {
7: countToAMillion();
8: document.getElementById('waitCursorLayer').style.display = "none";
9: }
The same technique can be applied to break out expensive operations to allow processing of the always important user events.
1: function countToAMillion(currentCount) {
2: // Count to a million in 10 incremets to ensure the UI events can be processed
3: var countBlockStarts = currentCount;
4: var countBlockEnds = countBlockStarts + 10;
6: for(var i = countBlockStarts; i < countBlockEnds; i++) {
7:
8: // Check to see if we are done
9: if(i >= 1000000) {
10: return;
11: }
12:
13: // Write the current count
14: document.getElementById("currentValue").innerHTML = i;
15: }
16:
17: // schedule the next few elements
18: window.setTimeout("countToAMillion(" + countBlockEnds + ")", 50);
19: }
Another trick that will come handy for most Widgets since they are generally full of dynamic content is that using innerHTML is significantly faster than creating the DOM elements individually and then appending them to the document. You should never append them to the document one by one, as this will completely destroy your performance since it will force the Widget to re-layout multiple times -- and this is really slow!. I know that this might not be possible in all cases but it is a nice tool to have in case your widget is having performance issues creating dynamic content.
Now let’s focus our attention to the flip side of performance which is, without a doubt, battery life. It is a fact of life that our Widgets, since they are executing code on the device, will have an impact on battery life. The goal is to minimize its effects to ensure happy users.
Thankfully the road to user happiness is not as rough as it might seem, there are a few principals that, if we keep them in mind while writing our Widget code, then we will know for sure that we are only using the power that is necessary to complete our task.
Example:
1: function onShow() {
2: // The widget was activated and is running in the foreground
3: // refresh the widget data
4: refreshWidgetData();
5: }
6:
7: function onHide() {
8: // Stop al recurring timers to be a good Widget citizen
9: stopAllTimers();
10: }
11:
12: function onLoad() {
13: var systemState = widget.createObject("SystemState");
14: var batteryStrength = systemState.PowerBatteryStrength;
15:
16: batteryStrength.addEventListener("changed", optimizeNetworkUsage);
17:
18: widget.onshow = onShow;
19: widget.onhide = onHide;
20: }
That’s it for now, but before I completely forget, one set of tools that will be your friend tracking performance issues on your widgets (and web applications as well) is the IE8 amazing developer tools (to start them, just press F12) which include a script profiler and it works great for widgets!
Next post: Widget Anatomy – Security
Previous post: Widget Anatomy – The Manifest This is part two of my “Widget Anatomy” series which will
Pingback from Twitter Trackbacks for Widget Anatomy ??? Performance and Battery Life - Windows Mobile Blog - The Windows Blog [windowsteamblog.com] on Topsy.com
Pingback from Windows Mobile 6.5 Widget anatomy: Performance and Battery Life « MobileTechWorld
Pingback from Widget Anatomy ??? Performance and Battery Life - Windows Mobile … Scripts Rss
Pingback from Strong Women ??? Weight Lifting has a Sweet Upside: Strength … | Fitness Health Wisdom
Pingback from South Glamorgan related services in North Yorkshire
Pingback from Widget Anatomy ??? Performance and Battery Life - Windows Mobile … Mobile CHN
I had two sessions to present at Over The Air 09 . The first was all about the new Widget runtime included
In part 3 of his series on Widgets for Windows Mobile 6.5, Jorge Peraza talks about how to write great
Pingback from Widget Anatomy ??? Security Insights - Windows Mobile Developer Blog - The Windows Blog
Pingback from You Get The . Info » Widget Anatomy ??? Security Insights – 1175th Edition
Pingback from Technology Info » Widget Anatomy ??? Security Insights
Pingback from Widget Anatomy ??? Security Insights | Voyager Enterprises
Pingback from MSDN Flash Podcast 019 – Paul Foster discusses Windows Mobile Widgets « IUpdateable from Eric Nelson