StorageFolder.CreateFileAsync crashes when called from App.OnSuspending

My Win RT application, which worked with VS2012RC on Windows 8 beta, now has the final versions of visual studio and windows 8, the problem is that creating / opening a file in OnSuspending only works if I set a debugger-breakpoint for 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 and if I continue, everything works fine.

  • However, if I do not set a breakpoint, the file will be created, but the xml content will not be saved (the file remains empty).

  • If I set a breakpoint below the line StorageFile file = await folder.CreateFileAsync(..., the debugger will never go in!

Anyone have an idea? I also tested the version that uses folder.OpenStreamForWriteAsync, with the same effect.

+5
2

Save. ( ), ( XML) , .

, , - . , OnSuspending aysnc, await ( 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);                    
    }           
}

, -, ( , - w8, , MS , , )...

+7

. 5 , , , . :

private async void OnSuspending(object sender, SuspendingEventArgs e)
{
    var deferral = e.SuspendingOperation.GetDeferral();
    try
    {
        await Task.Delay(1000);
        Debug.WriteLine("Done");
    }
    finally
    {
        deferral.Complete();
    }
}

. . . :

. , . GetDeferral SuspendingOperation ( args) , Complete SuspendingDeferral.

+3

All Articles