Waiting for async/await inside a task

C#.NetTask Parallel-LibraryTaskAsync Await

C# Problem Overview


I have this construct in my main(), which creates

var tasks = new List<Task>();

var t = Task.Factory.StartNew(
    async () =>
    {
        Foo.Fim();
        await Foo.DoBar();
    });

//DoBar not completed
t.Wait();
//Foo.Fim() done, Foo.DoBar should be but isn't

However, when I .Wait for t, it won't wait for the call to DoBar() to complete. How do I get it to actually wait?

C# Solutions


Solution 1 - C#

It's discouraged to use Task.Factory.StartNew with async-await, you should be using Task.Run instead:

var t = Task.Run(
    async () =>
    {
        Foo.Fim();
        await Foo.DoBar();
    });

The Task.Factory.StartNew api was built before the Task-based Asynchronous Pattern (TAP) and async-await. It will return Task<Task> because you are starting a task with a lambda expression which happens to be async and so returns a task. Unwrap will extract the inner task, but Task.Run will implicitly do that for you.


For a deeper comparison, there's always a relevant Stephen Toub article: Task.Run vs Task.Factory.StartNew

Solution 2 - C#

It seems like I get desired functionality by Unwrap()ing the task. I'm not quite sure I get the reasoning behind this, but I suppose it works.

var t = Task.Factory.StartNew(
            async () =>
                {
                        Foo.Fim();
                        await Foo.DoBar();
                }).Unwrap();

edit: I've looked for ddescription of Unwrap(): Creates a proxy Task that represents the asynchronous operation of a Task<Task<T>> I thought this was traditionally what the task did, but if I need to call unwrap I suppose that's fine.

Solution 3 - C#

I faced similar issue recently and figured out that all you need to do is have DoBar() return some value and use .Result instead of wait.

var g = Task.Run(() => func(arg));

var val = g.Result;

This will wait for func to return its output and assign it to val.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNathan CooperView Question on Stackoverflow
Solution 1 - C#i3arnonView Answer on Stackoverflow
Solution 2 - C#Nathan CooperView Answer on Stackoverflow
Solution 3 - C#Siddharth ShakyaView Answer on Stackoverflow