在WinRT中访问该文件时的一些例外
-
12-11-2019 - |
题
当我访问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 Helper函数是快乐命名的ComarshalinterThreadInterfaceInstream()。但是,您清楚地使用托管代码。必要时,它是CLR对元帅指针的工作。它已经如此可靠,一致地返回.NET版本1.0,我从未见过摸索的情况。
这非常强烈地提示C#等待/异步管道或CLR的WinRT投影中的错误。特别是因为它是虚假的,这种元帅的错误应该是一致的。你没有什么可以修复自己。使用connect.microsoft.com门户网站报告错误,他们需要一个小型的repro项目,展示了问题。
唯一的解决方法现在可以仔细控制应用程序中的线程。只需使用您创建的同一线程上的对象即可避免此Mishap。这并不完全是您逃避错误的保证。否则,您可以预期的那种头痛,当您尝试使用预测试代码时。
不隶属于 StackOverflow