gpt4 book ai didi

c# - 如何修复错误 : Cannot call the requested method (GetBasicPropertiesAsync). 之前对此方法的调用正在挂起

转载 作者:太空宇宙 更新时间:2023-11-03 15:51:33 24 4
gpt4 key购买 nike

我正在使用此 GetBasicPropertiesAsync 方法在循环中一个一个地获取文件属性。但是我遇到了这个错误:

WinRT Information : Cannot call the requested method (GetBasicPropertiesAsync). A previous call to this method is pending and must return before the method can be called again.

我的主要功能有以下代码来枚举图片库中的所有文件和文件夹。

List<BaseStorage> listOfFiles = new List<BaseStorage>();
IReadOnlyList<StorageFolder> foldersList = await curFolder.MCMFolder.GetFoldersAsync();

// For each folder found ...
foreach (StorageFolder folder in foldersList)
{
listOfFiles.Add(new Folder(folder, parents));
}

// Enumerate all files in the Pictures library.
IReadOnlyList<StorageFile> fileList = await curFolder.MCMFolder.GetFilesAsync();
// For each file found ...
foreach (StorageFile file in fileList)
{
listOfFiles.Add(new Document(file));
}

return listOfFiles;

Folder和Document类继承BaseStorage类。

class BaseStorage
{
public BaseStorage(IStorageItem storageItem)
{

this.Name = storageItem.Name;
this.CreationDate = storageItem.DateCreated.ToString();

setModifiedDateAndOwner(storageItem);
}

private async void setModifiedDateAndOwner(IStorageItem storageItem)
{

// await Task.Delay(500);

Windows.Foundation.IAsyncOperation<BasicProperties> basicPropsTask = storageItem.GetBasicPropertiesAsync();

BasicProperties _basicProps = await storageItem.GetBasicPropertiesAsync();

this.ModifiedDate = _basicProps.DateModified.ToString();

string fileOwnerProperty = "System.FileOwner";
List<string> propertiesToFetch = new List<string>();
propertiesToFetch.Add(fileOwnerProperty);

IDictionary<string, object> props = await _basicProps.RetrievePropertiesAsync(propertiesToFetch);
this.Owner = props[fileOwnerProperty].ToString();

return;
}
}

class Document
{
public Document()
{
setSize();
}

private async void setSize()
{
BasicProperties _basicProps = await file.GetBasicPropertiesAsync();
ulong fileSizeInBytes = _basicProps.Size;
}
}

这里的问题是方法 setModifiedDateAndOwner 调用了 GetBasicPropertiesAsync 方法。甚至在这个方法完成之前,子类 - 文档调用 setSize 方法,该方法再次调用 GetBasicPropertiesAsync 方法。

这会导致异常发生。但是,由于线程的原因,行为不是很一致。

在调用其子类中的方法之前,如何确保基类中的方法 setModifiedDateAndOwner 已完成。

最佳答案

构造函数不能是async,但是there are some ways to work around that .

由于您使用的是继承,因此您不能直接使用上述文章中的工厂模式,但您可以将派生类的工厂模式与基类的异步初始化模式的一个版本结合起来:

class BaseStorage
{
protected BaseStorage(IStorageItem storageItem)
{
this.Name = storageItem.Name;
this.CreationDate = storageItem.DateCreated.ToString();

Initialization = setModifiedDateAndOwner(storageItem);
}

protected Task Initialization { get; private set; }

private async Task setModifiedDateAndOwner(IStorageItem storageItem)
{

}
}

class Document : BaseStorage
{
private Document(IStorageFile storageFile)
: base(storageFile)
{ }

public static async Task<Document> Create(IStorageFile storageFile)
{
var result = new Document(storageFile);

await result.Initialization;

return result;
}
}

这样,在实现 Document 和其他继承自 BaseStorage 的类时必须小心,但您只能正确使用这些类(您必须 await Task 以获取创建的实例)。

关于c# - 如何修复错误 : Cannot call the requested method (GetBasicPropertiesAsync). 之前对此方法的调用正在挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25602165/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com