I suggest adding a task manager with a rating system."
API Proposal
class Manager
{
record InfoOfTask(Task task, long rating);
int _maxCountsTask;
List<InfoOfTask> _lsTask = new List<InfoOfTask>();
public Manager(int maxCountsTask)
{
_maxCountsTask = maxCountsTask;
}
public void Add(Action action, int rating)
{
var task = new Task(action);
checked
{
long range = (long)rating + DateTimeOffset.UtcNow.ToUnixTimeSeconds();
_lsTask.Add(new InfoOfTask(task, range));
}
}
public void Run()
{
var en = _lsTask.ToArray().OrderBy(a => a.rating);
int count = 0;
var ls = new List<Task>();
foreach (var element in en)
{
count++;
element.task.ContinueWith(a =>
{
throw new Exception("Exception", a.Exception);
}, TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously);
if (count >= _maxCountsTask)
{
Task.WaitAny(ls.ToArray());
int taskCounts = ls.RemoveAll(a => (int)a.Status > 4);
count -= taskCounts;
}
element.task.Start();
ls.Add(element.task);
}
}
API Usage
class Cat
{
public void Go()
{
var manager = new Manager(10);
var random =new Random();
for (int i = 0; i < 100; i++)
{
object item = i;
int rating = random.Next(1, 1000);
manager.Add(() => Console.WriteLine($"item:{item} rating:{rating} thread:{Thread.CurrentThread.ManagedThreadId}"),rating);
}
manager.Run();
}
}
Background and motivation
I suggest adding a task manager with a rating system."
API Proposal
API Usage
Alternative Designs
No response
Risks
No response