user7127000
user7127000

Reputation: 3233

What is a proper situation for await Task.StartNew(() => ...)?

I was wondering whether someone could help me figure out a proper use for code like

var result = await Task.StartNew(() => ...); 

or

var result = await Task<T>.StartNew(() => ...); 

From what I understand, it would never make sense to have simply

await Task.StartNew(() => ...);

or

await Task<T>.StartNew(() => ...);

because if you don't need the result then you might as well fire-and-forget with

Task.StartNew(() => ...) 

or

Task.StartNew<T>(() => ...) 

which which is like

Thread A | ----- Stuff before the Task.StartNew ---- | ---- Stuff after the Task.StartNew ------------------
Thread B | ------------ ??? ------------------------ | The () => .... inside the Task.StartNew -------------

Can someone provide me with a real-life example of when this would be useful?

Upvotes: 0

Views: 198

Answers (1)

Paulo Morgado
Paulo Morgado

Reputation: 14846

None!

Unless you know exactly what and why you're doing it, you should never use Task.Factory.StartNew or Task<T>.Factory.StartNew with async-await.

Not awaitng on the result of a task does not mean that you don't care about the result value. It means you don't care if it completes with success or even if it completes.

Upvotes: 3

Related Questions