与this question相关,我试图实现一个使用WinRT API设置桌面墙纸的过程。为了模仿C#中的await
功能,我使用了TTask.Future
(link),如here和here所述。德尔福 - 使用TTask.Future异步调用WinRT API
我的实现看起来是这样的:
class procedure TUtilityWin.SetWallpaper(AFileName: String);
var
lStorageFile: IStorageFile;
liao_storagefile: IAsyncOperation_1__IStorageFile;
lFutureTask: IFuture<IAsyncOperation_1__IStorageFile>;
begin
//WinRT Implementation
if TUserProfile_UserProfilePersonalizationSettings.IsSupported then
begin
lFutureTask:=TTask.Future<IAsyncOperation_1__IStorageFile>(
function: IAsyncOperation_1__IStorageFile
begin
Result:=TStorageFile.GetFileFromPathAsync(HSTRING(AFileName));
end);
liao_storagefile:=lFutureTask.Value;
lStorageFile:=liao_storagefile.GetResults;
TUserProfile_UserProfilePersonalizationSettings.Current.TrySetWallpaperImageAsync(lStorageFile);
end;
end;
按我的理解,当我试图让lFutureTask.Value
,应用程序挂起当前线程,直到lFutureTask完成(如果没有的话),然后提供的价值。但是,当我运行该应用程序时,出现错误消息:EOleException with message 'A method was called at an unexpected time'
。休息是在这条线上:lStorageFile:=liao_storagefile.GetResults;
我是TTask以及WinRT API的新手 - 所以我确定我缺少一些非常基本的东西。将不胜感激任何指针什么会导致这一点,或者我可以做什么不同来解决这个问题。提前致谢。
我可能会误解,但'IAsyncOperation_1__IStorageFile'似乎已经代表一个异步操作,您可能会嵌套它们。尝试省略'IFutureTask'并使用'IAsyncOperation_1__IStorageFile'的'Completed'事件。 – nil