2

I'm using this async code for my project:

public async Task<IEnumerable<Area>> GetAreas()
{

        var webclient = new WebClient();
        var result = await webclient.DownloadStringTaskAsync(new Uri(uri)).ConfigureAwait(false);

        return JsonConvert.DeserializeObject<IEnumerable<Area>>(result);
}


public async Task<ActionResult> Index()
    {
        var areas = GetAreas();
        Task.WaitAll(areas);
        return View(model: areas);
    }

I have tried this but when i debug my program areas variable have this properties: status:waiting for activation, method:"", Result : "". I know this happen because deadlock async.

please help me to solve my problem.

Community
  • 1
  • 1
Faisal Silitonga
  • 423
  • 1
  • 7
  • 14

1 Answers1

0

How about this :

public Task<ActionResult> Index()
{
    var tcs = new TaskCompletionSource<ActionResult>();
    GetAreas().ContinueWith(t => 
    {
      if(t.IsCancelled)
        tcs.SetCancelled();
      else if(t.IsFaulted)
        tcs.SetException(t.Exception);
      else
        tcs.SetResult(new View(model: t.Result); 
    });
    return tcs.Task;
}
atomaras
  • 2,468
  • 2
  • 19
  • 28