在OnSuspending方法的过程中保存数据 [英] Saving data during the OnSuspending method
问题描述
对于C / XAML在Windows 8应用我有一些数据存储到LocalState文件夹。我在使用异步一些麻烦/等待操作者,我认为。
For a Windows 8 Application in C/XAML I have to store some data to the LocalState folder. I have some trouble using async/await operator I think.
在应用程序运行时,当我保存DATAS,一切工作正常,但是当我尝试OnSuspending方法中保存数据,似乎我的应用程序不会等待我的节能方法,完成暂停应用程序。
When I am saving datas while the application is running, everything works fine, but when I try to save the data during the OnSuspending method, it seems that my application doesn't wait for my saving method to be finished to suspend the application.
这个陌生人的事情是,当我调试,一步完成所有操作一步慢慢一切工作正常,但是当我不把任何断点,保存数据之前应用程序被关闭。
The stranger thing is that when I'm debugging and doing all the operation step by step slowly everything works fine, but when I don't put any breakpoints, the application is closed before the data are saved.
下面是我的code:
private async void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
await api.BeforeClosing(true);
deferral.Complete();
}
public async Task BeforeClosing(Boolean toTombstoning)
{
SaveItem<LoadingProgress>(fileNameLoadingProgress, InitLoading);
}
public static async void SaveItem<T>(String fileName, T data, Boolean crossThreadSecure = false) where T : IBinarySerializable
{
await CreateNewStorageFile(fileName);
StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFolder dataFolder = await localFolder.GetFolderAsync("Data");
StorageFile file = await dataFolder.GetFileAsync(fileName);
using (var stream = await file.OpenStreamForWriteAsync())
{
using (var writer = new BinaryWriter(stream))
{
writer.Write<T>(data);
}
}
}
public async static Task CreateNewStorageFile(string filename)
{
StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFolder dataFolder = await localFolder.CreateFolderAsync("Data", CreationCollisionOption.OpenIfExists);
storage = await dataFolder.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists);
}
我做得不对的运营商的await?或者是那里,当我们可以暂停应用程序之前做一些操作的最大时间量?
Am I doing something wrong with the await operator? Or is there a maximum amount of time when we can do some operations before suspending the application?
感谢您
编辑:我做了一些新的考验:如果我删除deferral.Complete();从OnSuspending方法,这样可以节省数据很好(但从未显然关闭应用程序...)。我真的觉得问题是,我跑出来的时候,应用程序挂起。这真的看起来像在此线程的问题:<一href=\"http://stackoverflow.com/questions/11996288/storagefolder-createfileasync-crashes-when-called-from-app-onsuspending\">StorageFolder.CreateFileAsync从App.OnSuspending 调用时崩溃。但我已经试过了这个问题的全部解决方案,我仍然有问题...
EDIT : I made some new tests : If I remove deferral.Complete(); from the OnSuspending method, it saves the data well (but never close the app obviously...). I really think the problem is that I run out of time when the application is suspending. It really looks like the problem on this thread : StorageFolder.CreateFileAsync crashes when called from App.OnSuspending. But I have tried all the solutions of this question and I still have the problem...
推荐答案
您的问题是,你打电话完成
之前的所有异步
操作完成。
Your problem is that you're calling Complete
before all your async
operations have completed.
最简单的办法是让 SaveItem
的异步任务
方法,而不是异步无效
,然后等待
它 BeforeClosing
。
The simplest solution is to make SaveItem
an async Task
method instead of async void
, and then await
it in BeforeClosing
.
作为一般规则,异步
方法应该总是返回工作
/ 任务&LT; T&GT ;
除非有无的返回无效
(例如,事件处理程序)
As a general rule, async
methods should always return Task
/Task<T>
unless they have to return void
(e.g., event handlers).
这篇关于在OnSuspending方法的过程中保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!