DIFFERENCE BETWEEN AWAIT AND TASK.WAIT

Tanuj Kumar

Total Post:134

Points:940
Posted by  Tanuj Kumar
C# 
C#
 3470  View(s)
Ratings:
Rate this:

First way:

var tds=SearchProcess();

await tds;

public async  Task<XmlElement> SearchProcess()

{

}

Second way:

var tds= Task.Factory.StartNew(()=>SearchProcess());

Task.WaitAll(tds);

public XmlElement SearchProcess()

{

}

In above both approach any performance difference is there?

  1. Alex Leblois

    Post:67

    Points:471
    Re: Difference between await and Task.Wait

    Hi Tanuj,

    Task.WaitAll is blocking, while using await will make the containing method async. To wait for multiple tasks asynchronously you can use Task.WhenAll:

    public async Task DoSomething()

    {

        IEnumerable<Task> tds = SearchProcess();

        await Task.WhenAll(tds);

        //continue processing   

    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!