gpt4 book ai didi

windows-8 - 在 winrt 中访问文件时出现一些异常

转载 作者:行者123 更新时间:2023-12-04 21:22:39 26 4
gpt4 key购买 nike

我在winrt中访问文件时遇到一些问题

问题1:

var file = await StorageFile.GetFileFromPathAsync(filePath);

有时 GetFileFromPathAsync 会抛出“rpc 服务器不可用”异常。

问题2:
MusicProperties musicProp = await file.Properties.GetMusicPropertiesAsync();

有时它会抛出异常:
Unable to cast COM object of type 'Windows.Storage.FileProperties.MusicProperties' to interface type 'Windows.Storage.FileProperties.IMusicProperties'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{BC8AAB62-66EC-419A-BC5D-CA65A4CB46DA}' failed due to the following error: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD)).

问题3:
QueryOptions query = new QueryOptions(CommonFileQuery.OrderByMusicInfo, extensionList);
StorageFileQueryResult queryResult = folder.CreateFileQueryWithOptions(query);
IReadOnlyList<IStorageFile> files = await queryResult.GetFilesAsync();

有时它会抛出异常:
Unable to cast COM object of type 'Windows.Storage.StorageFile' to interface type 'Windows.Storage.IStorageFile'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{C7034384-F12E-457A-89DA-69A5F8186D1C}' failed due to the following error: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD)).

这些异常不会一直抛出,但有时会抛出。为什么?

最佳答案

这是由线程引起的那种问题,它是一个 COM 错误消息。并非完全出乎意料,WinRT 是基于 COM 的。错误消息说的是在一个线程上创建的接口(interface)指针正在另一个线程上使用而没有被编码(marshal)。

这是您在编写原始 COM 代码时通常必须自己做的事情。底层的 COM 帮助函数是命名为 CoMarshalInterThreadInterfaceInStream()。但是,您显然使用的是托管代码。在必要时编码指针是 CLR 的工作。一直到 .NET 1.0 版,它一直如此可靠且始终如一地完成,我从未见过它失败的案例。

这非常强烈地暗示了 C# 等待/异步管道或 CLR 的 WinRT 投影中的错误。特别是因为它是虚假的,所以这种编码错误应该是一致的。没有什么是你可以自己解决的。使用 connect.microsoft.com 门户报告错误,他们需要一个演示问题的小型重现项目。

您现在唯一可用的解决方法是仔细控制应用程序中的线程。通过仅在您创建它的同一线程上使用该对象来避免这种事故。这并不能完全保证您会避开该错误。否则,当您尝试使用 pre-beta 代码时,您可能会感到头疼。

关于windows-8 - 在 winrt 中访问文件时出现一些异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9443179/

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