Reputation: 4849
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.
Upvotes: 3
Views: 2295
Reputation: 24167
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.
Upvotes: 10
Reputation: 4849
Got it.. This will create an empty task:
concurrency::create_task([]{});
Upvotes: 2