KDecker
KDecker

Reputation: 7128

What does a method "do" when it awaits an empty Task?

I am outlining a class and to keep myself happy I have made a temporary return for a method

public override Task DoPostProcessing()
{
    return Task.Factory.StartNew(() => { ;} );
}

If I were to call this method and await it, what "happens"? Is this Task optimized away at compile time or is it ran?

Upvotes: 1

Views: 142

Answers (1)

Stephen Cleary
Stephen Cleary

Reputation: 456407

It is run. It will schedule a method on a thread pool that just returns immediately.

On a side note, do not use StartNew; it is dangerous. Use Task.Run to run code on a thread pool thread, or just use Task.FromResult to return an already-completed task. Task.FromResult should be your go-to choice for noop implementations.

Upvotes: 3

Related Questions