I'm in need of a solution that runs constantly incoming requests in a per-resource sequence, but parallel in general.
The use-case:
Many clients connect to a server and start issuing work. The work of a single client needs to run in sequential order, so the downward code doesn't need to cope with concurrency, but in general all work should be run on multiple threads. I'm trusting the .NET framework a lot here, which I hope is a good thing.
I've also read into DataFlow
and parallel Rx but could not find a general solution there. But hints into that direction are welcome!
class TaskGroup
{
public int CurrentlyQueuedTasks { get { return _currentlyQueued; } }
private readonly object _previousTaskMonitor;
private Task _previousTask;
private int _currentlyQueued;
public TaskGroup()
{
_previousTaskMonitor = new object();
_previousTask = Task.CompletedTask;
}
public void Append(Action action)
{
lock(_previousTaskMonitor)
{
Interlocked.Increment(ref _currentlyQueued);
_previousTask = _previousTask.ContinueWith(task =>
{
try
{
action();
}catch(Exception)
{
//TODO
}
finally
{
Interlocked.Decrement(ref _currentlyQueued);
}
});
}
}
}