When i execute the code, inspecting after WhenAll
the idList
is empty, if i step through it using breakpoints after the WhenAll
everything works as expected.
This code is within a non-async method.
Its as if letting it run on its own is not starting the tasks.
var currentPage = 1;
List<Result> idList = new List<Result>();
var response = this.GetDocuments(currentPage).Result;
//idList.AddRange(response.results);
var tasks = new List<Task>();
for (var i = 0; i < response.meta.page.total_pages; ++i)
{
var t = new Task(() =>
{
response = this.GetDocuments(i+1).Result;
idList.AddRange(response.results);
});
tasks.Add(t);
t.Start();
}
Task.WhenAll(tasks).Wait();
public async Task<ResponseObject> GetDocuments(int currentPage)
{
var result = await httpClient.GetAsync($"api/as/v1/engines/urlname/documents/list?page[current]={currentPage}").ConfigureAwait(false);
if (!result.IsSuccessStatusCode)
{
loggly.Error($"Exception: ScheduledJobBase, IndexPages, failed when getting documents from Swiftype, response: { result }");
return null;
}
return await result.Content.ReadAsAsync<ResponseObject>().ConfigureAwait(false); ;
}