I have an application that at startup starts about 120 async requests and it takes about 20-30 seconds to complete them all.
If I in that time, will do something in my application that also is set to run async with the "await" keyword it will be added last on the list and not run until the first 120 async requests are done.
What I want is to code a manager that can handle these requests and prioritize them.
I've read about the RunAsync method but the documentation say it's for Windows 8 minimum and I'm running on Windows 7 with .NET Framework 4.5.
My code right now looks like the following example:
private async void UpdateWebSiteInfo(int id)
{
InternetApi.WebSiteItem item = await Task.Run(() => InternetApi.WebSite.GetInformation(id));
// Handle the item
}
Since all isn't started from the same place or at the same time I don't know how I can give them some priority.
I've been thinking about having a manager-class that have a method like the following.
public async Task<TResult> Run<TResult>(Func<TResult> function, Priority priority)
{
// Wait if others are running but start if priority is "high"
}
And then you call that method the reqular way with await like:
var item = await this.priorityManager.Run(() => this.DoWork(i), PriorityManager.Priority.High);
// handle the item
But I'm having trouble how to code the implementation.
In this case it's about reading source-codes from web-pages but it could be to read files on the computer or calculate advanced algorithms and I would like them all to be able to use the same manager.
Would it be possible to do a solution like this or can someone point me in a better direction?