Consider the following C# code:

async Task DoSomethingAsync()
{
 if (m_f)
   return;
 await DoSomethingInternalAsync();
}

What the compiler turns this into is a task returning call where if m_f is true, the task completes immediately and if not, it "delegates" the async operation to DoSomethingInternalAsync().

Now, how do I do this in c++? The code should look something like this:

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

Edit1: In C#, I can use a TaskCompletionSource<> to do the same thing, but w/o using the async keyword - essentially create a completed Task.

有帮助吗?

解决方案

Another way to do this is task_from_result. You can use concurrency::task_from_result() for task<void> methods and concurrency::task_from_result(theReturnValue) for task<T> methods. I believe this is new starting in Visual Studio 2013.

其他提示

Got it.. This will create an empty task:

concurrency::create_task([]{});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top