[Solved] Call async code in an Action in C# [duplicate]


You need to mark your action as async just like you would any method, for example:

Action action = async delegate() 
    //snip

However, this is equivalent to an async void method which is not recommended. Instead you many consider using Func<Task> instead of action:

Func<Task> doStuff = async delegate()
{
  using (var client = new HttpClient())
  {
    var page = "http://en.wikipedia.org/";
    using (var response = await client.GetAsync(page))
    {
      using (var content = response.Content)
      {
        var result = await content.ReadAsStringAsync();
      }
    }
  }
}

Now you can await the result:

await doStuff();

3

solved Call async code in an Action in C# [duplicate]