Pages

Sunday, April 24, 2022

c# async/await

 Async/Await for c#. 

Two benefits:

  • UI is not locked up because control is returned to the calling method.
  • If the async calls do not depend on each other, then they can be run in parallel.

Parallel example:

Use a foreach and add Task to a List of Tasks. 

var tasks = new List<Task<string>>();

foreach (string site in listOfWebsites)
{
    tasks.Add(Task.Run( () => DownloadWebsite(site)));
}

var results = await Task.WhenAll(tasks);

No comments:

Post a Comment