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”));

05-04 03:47