void DirectXPage::Init(Platform::String^ strType)
{
OpenFile^ fopen = ref new OpenFile();
fopen->metro_fopen();
fopen->localFile->Path; //异常,因为 fopen->metro_fopen() 里面是异步操作
}
我需要等待 StorageFile^ s2 =fopen->metro_fopen(); 执行完成,如果异步它会继续执行到 s2->Path; 导致异常
求助该怎么做同步,代码呈上,求指教。
fopen->metro_fopen()
是个打开文件的操作:
StorageFile^ OpenFile::metro_fopen()
{
FileOpenPicker^ picker = ref new FileOpenPicker();
picker->SuggestedStartLocation = PickerLocationId::VideosLibrary;
picker->FileTypeFilter->Append(".wmv");
picker->FileTypeFilter->Append(".mp4");
create_task(picker->PickSingleFileAsync()).then(
[this](StorageFile^ inputFile)
{
localFile = inputFile; //取打开文件的 StorageFile 对象
if(localFile != nullptr)
{
return inputFile->OpenAsync(FileAccessMode::Read);
}
}).then(
[this](IRandomAccessStream^ inputStream)
{
}
});
}
}
}