首页 > 代码库 > AsyncOperation变成同步的代码

AsyncOperation变成同步的代码

template <typename TResult>
TResult PerformSynchronously(Windows::Foundation::IAsyncOperation<TResult>^ asyncOp)
{
Concurrency::event synchronizer;
Concurrency::task<TResult>(asyncOp).then([&](TResult taskResult) {
synchronizer.set();
}, Concurrency::task_continuation_context::use_arbitrary());
synchronizer.wait();
return asyncOp->GetResults();
}

 

用法:

ContactStore^ store = PerformSynchronously(ContactManager::RequestStoreAsync());
IVectorView<Windows::ApplicationModel::Contacts::Contact^>^ contacts = PerformSynchronously(store->FindContactsAsync(“123123”));

 

AsyncOperation变成同步的代码