Saved files sometime only contains NUL-characters

我与影子孤独终老i 提交于 2019-12-03 15:30:40

The only reasons I can think of that may produce this result are:

  • The OS hard crashes (BSOD) in the middle of a file write
  • The application is terminated in the middle of a file write

I'm assuming it's the second reason.

Given this and the fact that you say it's a windows runtime application and I'm guessing the application is not being terminated manually, my guess is that it's something to do with the windows runtime lifecycle. Given that UWP application can be "suspended" when it goes to the background and then may be terminated by the OS at any point while in the "suspended" mode (very similar to mobile OSes like on Android or IOS).

So given this, my guess is that your application starts async operations before it gets suspended, then it gets suspended / terminated before it completes your file writing.

The fix is to make sure you mark your app saying it's doing "background" work while it's doing your file writes so that the OS will not suspend you during your file writing. Pretty much all mobile OSes have this type of feature to allow your to "overrun" just a little to make sure you can get your work done before your app is suspended / terminated. For UWP apps check out background tasks or look into how to handle app suspend / postponing app suspend.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!