My Win RT application which has worked with VS2012RC on a Windows 8 beta, has now with the final versions of visual studio and windows 8 pro the problem, that creating/opening a file within OnSuspending only works if I set a debugger-breakpoint to the file creation method.
private void OnSuspending(object sender, SuspendingEventArgs e){
var deferral = e.SuspendingOperation.GetDeferral();
if (null != m_document) Save();
deferral.Complete();
}
async void Save(){
var folder = KnownFolders.DocumentsLibrary;
var file = await folder.CreateFileAsync(GetFileName(),Windows.Storage.CreationCollisionOption.ReplaceExisting);
var xDoc = GetXDocument();
using (var stream = await file.OpenStreamForWriteAsync()){
xDoc.Save(stream);
}
}
If I set a breakpoint on StorageFile file = await
folder.CreateFileAsync(...
, the debugger enters the and if I
continue, all works fine.
However if I dont set a breakpoint, the file will be created, but the content of the xml will not be saved (the file rests empty).
If I set a breakpoint below of the line StorageFile file = await folder.CreateFileAsync(...
, the debugger never enters!
Has anyone an idea? I have also tested a version which uses folder.OpenStreamForWriteAsync
, with the very same effect.
The problem was the call to the Save-method. Only the first part (creation of the file) was been waited for, the second part (saving XML) was done async, and therefore the deferral of the suspending operation has not been until the end of the save process.
A possible solution to avoid this problem is to wait explicitely for the completion of the save-operation. This can be accomplished by declaring the OnSuspending-method as aysnc
and then waiting for the completion of the save Operation with the await keyword (please note the Task return-type of the Save-method).
private async void OnSuspending(object sender, SuspendingEventArgs e){
var deferral = e.SuspendingOperation.GetDeferral();
if (null != m_document) await Save();
deferral.Complete();
}
async Task Save(){
var folder = KnownFolders.DocumentsLibrary;
var file = await folder.CreateFileAsync(GetFileName(),Windows.Storage.CreationCollisionOption.ReplaceExisting);
var xDoc = GetXDocument();
using (var stream = await file.OpenStreamForWriteAsync()){
xDoc.Save(stream);
}
}
I hope this post helps someone else who has been fallen into the same pitfall (I'm wondering why the problem has not occured with the beta of w8, but I think that MS has optimized the application termination-process and therefore there is less time for unexpected work after the suspension-process)...