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

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

问题描述

我的 Win RT 应用程序在 Windows 8 beta 上与 VS2012RC 一起工作,现在在 Visual Studio 和 Windows 8 pro 的最终版本中出现了问题,只有在我设置调试器时才能在 OnSuspending 中创建/打开文件-文件创建方法的断点.

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 file = await 上设置断点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 file = await 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.

      推荐答案

      问题是对 Save 方法的调用.仅等待第一部分(创建文件),第二部分(保存 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)...

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

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