Saving data during the OnSuspending method

混江龙づ霸主 提交于 2019-12-06 02:36:36

Your problem is that you're calling Complete before all your async operations have completed.

The simplest solution is to make SaveItem an async Task method instead of async void, and then await it in BeforeClosing.

As a general rule, async methods should always return Task/Task<T> unless they have to return void (e.g., event handlers).

The short and surprising answer is that OnSuspending is an unreliable place for any application critical persistence logic. For some reason Microsoft made the decision to delay the OnSuspending event for 5-10 seconds after an application has been closed. If the application is re-launched prior to this event a separate instance of the application is spawned and it is unaware of the one that is still in the process of suspending.

I came to this conclusion after having problems following what seemed like very simple examples in the WinRT Application Data Sample project: http://code.msdn.microsoft.com/windowsapps/ApplicationData-sample-fb043eb2. After some frustration I found a thread on MSDN that discusses OnSuspending as a trigger for saving: http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/43da2212-9910-461d-816a-94b479ce9885/. The gist of this thread is that you should save as often and frequently as possible because OnSuspending is unreliable.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!