I have isolated the following code and it works inside the OnNavigatedTo event, so I know the code works. BUT, I can't use it there. I need to use it inside the Suspending event. But it won't work there. And when I set breakpoints, they're not being hit anywhere inside this event. There are no compiletime or runtime errors either.
我已经隔离了以下代码,它在OnNavigatedTo事件中工作,所以我知道代码有效。但是,我不能在那里使用它。我需要在Suspending事件中使用它。但它不会在那里工作。当我设置断点时,它们不会在此事件中被击中。也没有编译时或运行时错误。
What is going on?
到底是怎么回事?
async void App_Suspending(
Object sender,
Windows.ApplicationModel.SuspendingEventArgs e)
{
IReadOnlyList<StorageFile> thefiles;
var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Globalization.DateTimeFormatting.DateTimeFormatter formatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("longtime");
thefiles = await localFolder.GetFilesAsync();
foreach (var f in thefiles)
{
await f.DeleteAsync(StorageDeleteOption.Default);
}
}
2 个解决方案
#1
3
My guess is that when you await in this method the app exits the Suspending method and in this way gives the OS permission to kill the process. You can test this by putting a breakpoint after the first await (on the foreach) and checking if it is ever reached.
我的猜测是,当你等待这个方法时,应用程序退出Suspending方法,并以这种方式授予操作系统杀死进程的权限。您可以通过在第一次等待(在foreach上)之后放置断点并检查是否到达它来测试这一点。
#2
0
I have found my solution. It involved checking whether or not the app was closed by the user. If so, it is (in my case, anyway) OK to delete these temporary files. You can do this in the App.xaml.cs file inside the OnLaunched method:
我找到了解决方案。它涉及检查用户是否关闭了应用程序。如果是这样,那么(在我的情况下,无论如何)可以删除这些临时文件。您可以在OnLaunched方法内的App.xaml.cs文件中执行此操作:
if (args.PreviousExecutionState == ApplicationExecutionState.ClosedByUser)
{
IReadOnlyList<StorageFile> thefiles;
var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Globalization.DateTimeFormatting.DateTimeFormatter formatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("longtime");
thefiles = await localFolder.GetFilesAsync();
foreach (var f in thefiles)
{
await f.DeleteAsync(StorageDeleteOption.Default);
}
}
#1
3
My guess is that when you await in this method the app exits the Suspending method and in this way gives the OS permission to kill the process. You can test this by putting a breakpoint after the first await (on the foreach) and checking if it is ever reached.
我的猜测是,当你等待这个方法时,应用程序退出Suspending方法,并以这种方式授予操作系统杀死进程的权限。您可以通过在第一次等待(在foreach上)之后放置断点并检查是否到达它来测试这一点。
#2
0
I have found my solution. It involved checking whether or not the app was closed by the user. If so, it is (in my case, anyway) OK to delete these temporary files. You can do this in the App.xaml.cs file inside the OnLaunched method:
我找到了解决方案。它涉及检查用户是否关闭了应用程序。如果是这样,那么(在我的情况下,无论如何)可以删除这些临时文件。您可以在OnLaunched方法内的App.xaml.cs文件中执行此操作:
if (args.PreviousExecutionState == ApplicationExecutionState.ClosedByUser)
{
IReadOnlyList<StorageFile> thefiles;
var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Globalization.DateTimeFormatting.DateTimeFormatter formatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("longtime");
thefiles = await localFolder.GetFilesAsync();
foreach (var f in thefiles)
{
await f.DeleteAsync(StorageDeleteOption.Default);
}
}