当在不同的线程上多次调用 Windows.Storage.StorageFolder.GetFolderFromPathAsync 时,我收到 ArgumentExceptions。这是重现该问题的测试:
[TestMethod]
public async Task ConcurrentGetFolderFromPath()
{
List<Task> tasks = new List<Task>();
for (int i = 0; i < 10; i++)
{
var task = Task.Run(async () =>
{
string localFolderPath = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
//await Task.Yield();
var folder = await Windows.Storage.StorageFolder.GetFolderFromPathAsync(localFolderPath);
});
tasks.Add(task);
}
await Task.WhenAll(tasks);
}
这是我得到的例外:
System.ArgumentException: Value does not fall within the expected range.
Result StackTrace:
at Windows.Storage.StorageFolder.GetFolderFromPathAsync(String path)
at PCLStorage.Test.FolderTests.<<ConcurrentGetFolderFromPath>b__53>d__55.MoveNext() in c:\git\pclstorage\test\PCLStorage.Test\FolderTests.cs:line 205
当我自己运行它时,测试始终失败,但当我与其他PCL 存储测试一起运行时,它通常会通过。
我在这里做错了吗?GetFolderFromPathAsync 或我正在使用的任何其他 API 是否只应该在 UI 线程中使用?或者这可能是 WinRT 存储 API 中的错误?