加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

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

发布时间:2020-12-14 02:49:22 所属栏目:Windows 来源:网络整理
导读:我在winrt中访问文件时遇到一些问题 问题1: var file = await StorageFile.GetFileFromPathAsync(filePath); 有时,GetFileFromPathAsync将抛出“rpc服务器不可用”异常. 问题2: MusicProperties musicProp = await file.Properties.GetMusicPropertiesAsyn
我在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.错误消息说的是在一个线程上创建的接口指针正在另一个线程上使用而没有被编组.

这是您在编写原始COM代码时通常必须自己做的事情.底层的COM辅助函数是快乐命名的CoMarshalInterThreadInterfaceInStream().但是,您显然正在使用托管代码. CLR的工作是在必要时编组指针.它已经可靠而一致地一直回到.NET 1.0版本,我从来没有见过它丢失的情况.

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

您现在唯一可用的解决方法是小心控制应用中的线程.通过仅使用您创建它的同一个线程上的对象来避免这种不幸.这并不能完全保证您能够逃避错误.否则,当您尝试使用pre-beta代码时,您可能会遇到什么样的问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读