根据MSDN,
基于任务的延续总是安排在前面的任务完成时执行,即使前面的任务被取消或抛出异常也是如此。
我不明白这一点,因为我尝试了以下代码,并且当第一个任务通过抛出异常完成时不会调用后续任务。我理解为什么它必须将呼叫转发到被呼叫的站点,concurrency::task::wait
但我不明白 MSDN 上的声明是什么意思。我有什么误解?
#include <iostream>
#include <ppl.h>
#include <ppltasks.h>
int main(int argc, char* argv[])
{
using namespace std;
concurrency::task<void> task;
auto task_ticket = concurrency::create_task([]()
{
// A continuation task executed asynchronously
// after the previous task has completed. This
// is executed even if the previous task fails
// by being cancelled or throwing an exception.
throw std::runtime_error("Hello");
})
.then([]()
{
// This should be executed even though the
// previous task failed.
cout << "Task (2) executed" << endl;
});
try
{
task_ticket.wait();
}
catch (std::exception const& e)
{
cout << "Exception caught\n";
}
return EXIT_SUCCESS;
}