Click here to Skip to main content
13,047,502 members (61,532 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hy there,

I've just started develop apps to win8. Win8 app closing event does not exist, only suspended event. I've and app, that should log out from a database, when the user close the app. I have a WCF service that handle the database operations. I call the WCF service from suspending event, but it doesnt make any effect on database. I am sure that this works fine, if i call it from anywhere else.

Any help would be great, thanks in advance.

private async void Current_Suspending(object sender, Windows.ApplicationModel.SuspendingEventArgs e)
            await service.SetUserOnlineStatusAsync(UserId, 0);
Posted 18-Nov-12 0:53am
Updated 18-Nov-12 0:57am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I recently read about something similar and i did not try it out myself, but it seems to me like the runtime cancells your operation.

try notifying the runtime of your outstanding operation by requesting a SuspendingDeferral[^]:

private async void Current_Suspending(object sender, Windows.ApplicationModel.SuspendingEventArgs e) {
            var deferral = e.SuspendingOperation.GetDeferral();
            await service.SetUserOnlineStatusAsync(UserId, 0);

in any case you should be aware that the runtime will terminate anyway, if the "deadline for suspension has passed" - whatever timespan this is...if you find out, let me know ;)
Jason Gleim 19-Nov-12 11:10am
Earloc is right... Web Service calls are made on another thread to achieve the async nature of the call. Awaiting the call doesn't eliminate the extra thread... it just makes the whole thing synchronous without all the crappy workarounds. When the suspend event is raised, the runtime won't allow you to spin up additional threads... you are supposed to be suspending... not starting new work.

Suspend gets called whenever your app is no longer the focus. So if the user switches app via the charms flyout or the windows key, opens the start screen, closes the lid on the Ultrabook, whatever else... Windows will raise the suspending event to let you know. The idea there is that your app will QUICKLY persist any state information that it needs before being suspended. (as memory may get erased... object state is not guaranteed in the suspended state.)

Earloc is also correct that you can ASK for a deferral but it does NOT guarantee you will get one. I think there is a limit of either one or two deferrals before Windows will just suspend you anyway.

There is not a lot on this wrt Win 8 yet. But the same functionality has been around in WP7 since the beginning. There is a LOT written on it, the thinking behind it, and how to handle it. It should all be applicable to Win 8 as the mechanisms are very similar. I would suggest looking that direction for more information.

For Win8 RT apps though, you generally need to get away from the always-connected paradigm. Win 8, RT especially, runs much more like a mobile OS. A user could spin up your app, use it for 5 seconds, then task switch and you would get dehydrated. You could spend the next week that way before the user comes back to your app. You HAVE to be able to handle that situation. If there is a login component to your database, you may wish to consider an identity provider in between your app and the DB. Databases aren't good at user management and login sessions... identity providers are... and can marshal the security requirements of a call to a DB web service before it is made. A lot of web sites use OAuth 2 as a means of providing a renewable login token (persisted login) for API calls or web interfaces.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100