Handling application states on Windows 8
As heard on BuildWindows and over and over agai
n
throughout the sessions there is new application
state called Suspended.
When using the computer in Metro style mode the application
changes state to “suspended” when the user moves to another
application. This is mainly for power saving since Windows 8 targeted to portable devices such as tablets.
So, when developing an application we would like to do some operations when changing states. Let’s see how an application written in C# will handle it:
protected async void OnSuspending(object sender,
Windows.ApplicationModel.SuspendingEventArgs args)
{
SuspendingDeferral deferral = args.SuspendingOperation.GetDeferral();
await SuspensionManager.SaveAsync();
deferral.Complete();
}
the OnSuspending method is set as the event handler for the application suspending event. it receives a SuspendingEventArgs which has the data for the suspending event. one important part of it is the SuspendingDeferral class which manages the suspension operation.
the code calls a method called SaveAsync which saves the application state somewhere. then the deferral variable notifies the system that the application has done what it has to do and can be suspended.
another thing to keep in mind is that we see here allot of asynchronous operations. that’s because one of the main concerns on windows 8 is to have the ui as much responsive as possible. however, instead of placing a callback before calling an asynchronous operation we use the new await keyword, where’s the callback is actually the code coming after the async call.
you can use the await keyword before every method that has another keyword which is async. you can see that the onsuspending method has that keyword but also the saveasync as seen below:
static async public task saveasync()
{
await Windows.System.Threading.Threadpool.RunAsync((wiargs) =>
{
Suspensionmanager.SaveimplAsync().Wait();
}, Windows.System.Threading.WorkItemPriority.Normal);
}
And what about going back from suspension mode? well, that’s the same as being launched. therefore thee app.xaml class has overridden the OnLaunched method, than it simply does the opposite and restores the application’s state:
protected override async void OnLaunched(LaunchActivatedEventArgs args)
{
if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
// Do an asynchronous restore
await SuspensionManager.RestoreAsync();
}
//Set the main window
Window.Current.Content = new MainPage();
Window.Current.Activate();
}
apart from launching the application’s start page – you can see that when the application launches it checks for it’s state and restores when it the application somehow got terminated (aka – suspended).
the very implementation of RestoreAsync and SaveAsync differs of course between one application and another, that’s why i’ve chosen not to paste the entire implantation here on the code.
Summary:
Windows 8 tries not to encourage applications running side by side. therefore on the metro mode when switching from one application to another, the application you switched from becomes suspended, and simply won’t run.
therefore, upon suspension, you need to save your last state and use it upon re-launching to take the application where the point it was left off.
And – a little apology towards the end, This is my first post from my new Windows8 Tablet. It doesn’t work that well with Windows Live Writer so this post might not look so well. As being said on the Build keynote – this is a developer preview, not an RTM…
Windows 8 For developers - what’s new?
Today in BUILD, it was all about Windows 8 and it seems like a very big change from what we’ve known so far.
And what is the big change?
JavaScript & HTML have become a native part of windows application. That is something completely new for us. HTML comes out of the browser for windows 8 and it can be a UI for a “regular” windows application.
The Metro style applications, meaning that application doesn’t stand alone but it is a part of “Application web” as put by the keynote various speakers.
It means that if you write some text in a word processor and you want to translate it using a translation app, you can pick the translation app after selecting text instead of Copy –> Open translation app –> Paste –> translate.
This is done by contracts. These contracts defined by the OS and each application can choose whether and how to implement them.
This means that the applications share content, not only by the clipboard. The applications share content using contracts.The contract are predefined by the OS (Picker, Share & Search contracts)
To see the whole story let’s take a look at the “Windows 8 Platforms and tools” below:

The WinRT API is a native API but it can be easily used by C# & VB (easier than win32 API)
but moreover, by JavasScript. From this picture we can also see that a XAML application
can be written in C/C++ rather than just .Net language, that’s because XAML was re-implemented natively and now can serve native code as well.
To sum things up: Building application for windows has changed. The most important news, if you ask me, is the contracts that allows application to speak with one another (aka Metro-Style apps). The second most important is the ability to develop application using HTML & Javascript. There are of course some other things like the new App store, the fact that the apps I’ll post more about the development tools as the convention continues.
It’s BUILD time
Today I’m off to BUILD/Windows among a delegation of almost 20 experts from SELA.
As everyone, I’m looking forward for this conference. Hope to see you there.
You can find us with our orange shirts.
See you there,
Ran.
A very strange phenomenon I came across while developing web application for a customer.
A Jpeg formatted picture wasn’t shown on IE8 browsers, while shown on others (Firefox 5, Chrome 12, IE9).
The browser displayed an empty placeholder with a red ‘X’ as it does when not finding an image. This led as to searching for causes such as wrong URL, communication problems etc. .
After a while, one of our IT personnel have discovered that when requesting the image, there’s some XML inside the content delivered in the response.
The content injected looked like that:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.print.PageFormat.PMHorizontalRes</key>
<dict>
<key>com.apple.print.ticket.creator</key>
<string>com.apple.jobticket</string>
<key>com.apple.print.ticket.itemArray</key>
<array>
<dict>
<key>com.apple.print.PageFormat.PMHorizontalRes</key>
<real>72</real>
<key>com.apple.print.ticket.stateFlag</key>
<integer>0</integer>
</dict>
</array>
</dict>
<key>com.apple.print.PageFormat.PMOrientation</key>
<dict>
<key>com.apple.print.ticket.creator</key>
<string>com.apple.jobticket</string>
<key>com.apple.print.ticket.itemArray</key>
<array>
<dict>
<key>com.apple.print.PageFormat.PMOrientation</key>
<integer>1</integer>
<key>com.apple.print.ticket.stateFlag</key>
…
This content was injected somehow, by a graphic editor at the marketing company we’re working with, to the image file. It has been handled well at all new browsers but wasn’t
in IE8 which is still wide spread browser we have to support.
What can you do to solve this?
Simply open the same file in some basic graphic editor (Paint.Net will do, Windows Paint won’t) and re-save it. The content seen above will be removed.
In conclusion: An odd behavior by some graphic editor costs us some working hour. The solution for this is very simple and involved no coding. I hope this small post saved you the headache we’ve got.