StorageFolder.CreateFileAsync崩溃从App.OnSuspending调用时 [英] StorageFolder.CreateFileAsync crashes when called from App.OnSuspending

查看:127
本文介绍了StorageFolder.CreateFileAsync崩溃从App.OnSuspending调用时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已与VS2012RC在Windows 8测试版的工作赢得RT的应用,目前已与Visual Studio和Windows 8的最终版本亲的问题,在创建/打开内OnSuspending一个文件,如果我设置一个debugger-只适用断点文件的创建方法。

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);                    
    }           
}


  • 如果我设置 StorageFile文件断点=等待
    folder.CreateFileAsync(...
    ,调试器进入,如果我
    继续,一切工作正常。

    • If I set a breakpoint on StorageFile file = await folder.CreateFileAsync(..., the debugger enters the and if I continue, all works fine.

      但是,如果我不设置断点,该文件将被创建,但
      在XML内容不会被保存(文件休息空)。

      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).

      如果我设置为低于该行的断点 StorageFile文件=伺机folder.CreateFileAsync(... ,调试器不会进入!

      If I set a breakpoint below of the line StorageFile file = await folder.CreateFileAsync(..., the debugger never enters!

      有没有人的想法?我也测试了它使用 folder.OpenStreamForWriteAsync ,具有非常相同的效果的版本。

      Has anyone an idea? I have also tested a version which uses folder.OpenStreamForWriteAsync, with the very same effect.

      推荐答案

      的问题是在调用保存法。仅第一部分(创建文件)中被等待,第二部分(保存XML)做异步的,因此悬浮操作的推迟尚未直到保存过程结束。

      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.

      一个可能的解决方案,以避免这种问题是明确地等待保存操作的完成。这可以通过声明OnSuspending法为 aysnc ,然后等待保存操作完成的的await关键字(请注意任务返回式另存来完成-方法)。

      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);                    
          }           
      }
      

      我希望这篇文章可以帮助别人谁已陷入同样的​​陷阱(我不知道为什么这个问题一直没有和W8的beta发生,但我认为,MS优化了应用程序终止进程,因此有对于悬浮过程之后意想不到的工作时间更少)...

      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)...

      这篇关于StorageFolder.CreateFileAsync崩溃从App.OnSuspending调用时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆