我在尝试通过 "appFolder.GetFilesAsync();" 读取文件列表时不断出错
I keep getting errors while trying to read a list of files via "appFolder.GetFilesAsync();"
我一直关注 Debug.Window 的输出。
Der Thread 0xd88 hat mit Code 0 (0x0) geendet.
Der Thread 0x6fc hat mit Code 0 (0x0) geendet.
Der Thread 0xce8 hat mit Code 0 (0x0) geendet.
Der Thread 0x68c hat mit Code 0 (0x0) geendet.
我的代码是:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.UI.Xaml.Controls;
namespace WindowsIOTControlCenter.ImageRotation
{
class ImageRotation
{
Timer _time;
IReadOnlyList<StorageFile> pictures;
public ImageRotation(Grid targetGrid)
{
pictures = scanImagesFolder().Result;
}
private async Task<IReadOnlyList<StorageFile>> scanImagesFolder()
{
try
{
StorageFolder appFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("ImageRotation\BackgroundImages");
IReadOnlyList<StorageFile> filesInFolder = await appFolder.GetFilesAsync();
foreach (StorageFile file in filesInFolder)
Debug.WriteLine(file.Name + ", " + file.DateCreated);
return null;
}
catch(Exception ex)
{
Debug.WriteLine(ex.Message);
return null;
}
}
}
}
我使用了来自 inet 的一个非常基本的示例,因为我的原始代码与我现在在这个示例中遇到的问题相同。
我基本上想要一个位于指定目录中的文件列表。
逐步调试显示将目录分配给 appFolder 没有问题。
但是说到
IReadOnlyList<StorageFile> filesInFolder = await appFolder.GetFilesAsync();
上面提到的输出一步步退出。显然没有 Exception 可以捕获,否则它会继续 catch(Exception ex).
有没有人知道我做错了什么或者可以指出我的问题的另一种解决方案?
感谢任何提示。
P.S.: 抱歉我的英语很烂
尝试这样做:
appFolder.GetFilesAsync().ConfigureAwait(假)
错误的翻译是“线程 0xd88 以代码 0 (0x0) 结束”。这不是报错,程序得到这个信息是正常的。
当您执行 pictures = scanImagesFolder().Result;
时,您可能会通过在使用 await
.
的任务上调用 .Result
来导致程序死锁
您可以做的几件事。
- 使用
filesInFolder = await appFolder.GetFilesAsync().ConfigureAwait(false);
,这使得它不再尝试 运行 UI 线程上的其余代码,因此调用 .Result
是 不太可能陷入僵局。如果 GetFilesAsync
不同时使用 .ConfigureAwait(false)
你仍然可能死锁。
将您的代码移出构造函数并在您的方法中使用 await
而不是 .Result
class ImageRotation
{
Timer _time;
IReadOnlyList<StorageFile> pictures;
public ImageRotation(Grid targetGrid)
{
};
public async Task LoadImages()
{
pictures = await scanImagesFolder();
}
如果需要,这将使 pictures =
处于相同的上下文中。如果您不需要该功能,请使用
public async Task LoadImages()
{
pictures = await scanImagesFolder().ConfigureAwait(false);
}
请阅读“Async/Await - Best Practices in Asynchronous Programming”,它会教你不调用.Result
和避免async void
等基础知识
我一直关注 Debug.Window 的输出。
Der Thread 0xd88 hat mit Code 0 (0x0) geendet.
Der Thread 0x6fc hat mit Code 0 (0x0) geendet.
Der Thread 0xce8 hat mit Code 0 (0x0) geendet.
Der Thread 0x68c hat mit Code 0 (0x0) geendet.
我的代码是:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.UI.Xaml.Controls;
namespace WindowsIOTControlCenter.ImageRotation
{
class ImageRotation
{
Timer _time;
IReadOnlyList<StorageFile> pictures;
public ImageRotation(Grid targetGrid)
{
pictures = scanImagesFolder().Result;
}
private async Task<IReadOnlyList<StorageFile>> scanImagesFolder()
{
try
{
StorageFolder appFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("ImageRotation\BackgroundImages");
IReadOnlyList<StorageFile> filesInFolder = await appFolder.GetFilesAsync();
foreach (StorageFile file in filesInFolder)
Debug.WriteLine(file.Name + ", " + file.DateCreated);
return null;
}
catch(Exception ex)
{
Debug.WriteLine(ex.Message);
return null;
}
}
}
}
我使用了来自 inet 的一个非常基本的示例,因为我的原始代码与我现在在这个示例中遇到的问题相同。
我基本上想要一个位于指定目录中的文件列表。
逐步调试显示将目录分配给 appFolder 没有问题。
但是说到
IReadOnlyList<StorageFile> filesInFolder = await appFolder.GetFilesAsync();
上面提到的输出一步步退出。显然没有 Exception 可以捕获,否则它会继续 catch(Exception ex).
有没有人知道我做错了什么或者可以指出我的问题的另一种解决方案?
感谢任何提示。
P.S.: 抱歉我的英语很烂
尝试这样做: appFolder.GetFilesAsync().ConfigureAwait(假)
错误的翻译是“线程 0xd88 以代码 0 (0x0) 结束”。这不是报错,程序得到这个信息是正常的。
当您执行 pictures = scanImagesFolder().Result;
时,您可能会通过在使用 await
.
.Result
来导致程序死锁
您可以做的几件事。
- 使用
filesInFolder = await appFolder.GetFilesAsync().ConfigureAwait(false);
,这使得它不再尝试 运行 UI 线程上的其余代码,因此调用.Result
是 不太可能陷入僵局。如果GetFilesAsync
不同时使用.ConfigureAwait(false)
你仍然可能死锁。 将您的代码移出构造函数并在您的方法中使用
await
而不是.Result
class ImageRotation { Timer _time; IReadOnlyList<StorageFile> pictures; public ImageRotation(Grid targetGrid) { }; public async Task LoadImages() { pictures = await scanImagesFolder(); }
如果需要,这将使
pictures =
处于相同的上下文中。如果您不需要该功能,请使用public async Task LoadImages() { pictures = await scanImagesFolder().ConfigureAwait(false); }
请阅读“Async/Await - Best Practices in Asynchronous Programming”,它会教你不调用.Result
和避免async void