【发布时间】:2012-09-27 07:12:38
【问题描述】:
我需要在我声明的方法中调用 Async 方法。该方法应该返回一个值。我正在尝试将对 Windows 应用商店的调用包装到一个易于使用的类中。我的方法应该是这样的:
bool Purchase(enum_InAppOption optionToPurchase);
enum_InAppOption 是一个枚举,包含所有要购买的应用内选项。在某些时候我需要打电话给RequestProductPurchaseAsync。此调用的结果确定该方法是否应返回true 或false。我是 c++/cx 的新手(或者至少从现在到上次使用 c++ 之间我有很长的历史),所以我认为这可能更容易。
create_task 看起来像这样:
create_task(CurrentAppSimulator::RequestProductPurchaseAsync(this->_LastProductId, false))
我考虑/尝试过的选项:
返回任务不会抽象存储
尝试调用等待任务。我有例外
An invalid parameter was passed to a function that considers invalid parameters fatal.尝试使用
structured_task_group,但似乎这不允许非 void 返回方法,或者我试图提供错误的解释。编译器返回错误 C2064(已用 Google 搜索,但我无法理解要更改的内容)使用任务数组和
when_all
在页面中间的http://msdn.microsoft.com/en-us/library/dd492427.aspx#when_all找到如下代码:
array<task<void>, 3> tasks =
{
create_task([] { wcout << L"Hello from taskA." << endl; }),
create_task([] { wcout << L"Hello from taskB." << endl; }),
create_task([] { wcout << L"Hello from taskC." << endl; })
};
auto joinTask = when_all(begin(tasks), end(tasks));
// Print a message from the joining thread.
wcout << L"Hello from the joining thread." << endl;
// Wait for the tasks to finish.
joinTask.wait();
于是我尝试将其翻译成如下代码:
array<task<Platform::String^>,1> tasks = {
create_task(CurrentAppSimulator::RequestProductPurchaseAsync(this->_LastProductId, false))
};
即使我包含编译器也会抛出 C2065 ('array': undeclared identifier), C2275 ('Concurrency::task<_returntype>': 非法使用这种类型作为表达式和一些错误似乎是后续错误关于那两个。
总结一下:如何在异步任务完成后让方法返回,这样我就可以根据异步进行的东西返回有意义的结果?
【问题讨论】:
标签: c++ windows-8 c++-cx windows-store