I’m trying to write a method that reads each page of a pdf, but since it takes a significant amount of time to read each page through the api and I’m looking at pdfs hundreds of pages long, I want to queue the reading of each page asynchronously and then return the results when they’re ready, so multiple pages are being read at once.
I use Task.Run to “queue” the task, and I expect to see the Debug log print the pages out of order, but they only execute in order, so I think they are being run synchronously. Any ideas?
var tasks = new List<Task>();
foreach (Page page in _pdfDoc.GetPages()) {
var task = Task.Run(() => {
//tried adding await Task.Yield() here, doesn't work
Debug.WriteLine("searching page " + page.Number);
if (page.Text.Contains(query)) {
pagesWithQuery.Add(page.Number);
}
howManySearched += 1;
Dispatcher.UIThread.InvokeAsync(() => {
searchProgress.Value = howManySearched;
});
return Task.CompletedTask;
});
tasks.Add(task);
// await task; <== does nothing??
}