Handling application states on Windows 8

16 בSeptember 2011

tags: ,
no comments

Handling application states on Windows 8

As heard on BuildWindows and over and over agaiBuild/Windowsn
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();

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) =>
            }, 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();

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.


 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…

kick it on DotNetKicks.com

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>