在VC++ PPL中,如何创建一个同步返回的任务返回方法?

Sha*_*ish 3 c++ task-parallel-library windows-8 windows-runtime ppl

考虑以下C#代码:

async Task DoSomethingAsync()
{
 if (m_f)
   return;
 await DoSomethingInternalAsync();
}
Run Code Online (Sandbox Code Playgroud)

编译器将其转换为一个任务返回调用,如果m_f为true,则任务立即完成,如果不是,则将"异步"操作"委托"给DoSomethingInternalAsync().

现在,我如何在c ++中执行此操作?代码看起来应该是这样的:

task<void> DoSomethingAsync()
{
if (m_f)
   return [[What do I return here so the task is complete (.then called immediately)?!]];
return DoSomethingInternalAsync();
}
Run Code Online (Sandbox Code Playgroud)

Edit1:在C#中,我可以使用TaskCompletionSource <>来做同样的事情,但是没有使用async关键字 - 实质上是创建一个完成的Task.

bob*_*mcr 8

另一种方法是task_from_result.您可以使用concurrency::task_from_result()task<void>方法和concurrency::task_from_result(theReturnValue)task<T>方法.我相信这是Visual Studio 2013的新起点.