Can't get result from task list

Viewed 6623

I have this async function that returns a Task

public async Task<SettingModel> GetSetting(string key)
{
    var rootPath = _hostingEnvironment.ContentRootPath;
    using (StreamReader r = new StreamReader(rootPath + key + "settings.json"))
    {
        string json = await r.ReadToEndAsync();
        var settings = JsonConvert.DeserializeObject<SettingModel>(json);
        return settings;
    }
}

Now I want to get all settings and then wait until all is completed like this

public async Task GetData(List<string> keys)
{
    var taskList = new List<Task>();
    foreach(var key in keys)
    {
        taskList.Add(GetSetting(key));
    }

    await Task.WhenAll(taskList.ToList());

    foreach (var task in taskList)
    {
        task.Result // here its not working. The task don't have a result :(
    }
}

How to get the data from the task?

1 Answers
Related