Click here to Skip to main content
15,867,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

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

1 solution

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[^]:


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


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 ;)
 
Share this answer
 
v2
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900