Skip to content

Instantly share code, notes, and snippets.

@EngRajabi
Created September 17, 2020 12:19
Show Gist options
  • Save EngRajabi/ec04cf7f95dac84b030d7f87e716f4e6 to your computer and use it in GitHub Desktop.
Save EngRajabi/ec04cf7f95dac84b030d7f87e716f4e6 to your computer and use it in GitHub Desktop.
Workers.cs
// add to di singleton
public class Workers
{
private ConcurrentBag<IDataflowBlock> Actions { get; set; } = new ConcurrentBag<IDataflowBlock>();
public ActionBlock<TInput> AddWork<TInput>(Func<TInput, Task> work) where TInput : class, new()
{
var action = new ActionBlock<TInput>(work);
Actions.Add(action);
return action;
}
public ActionBlock<TInput> AddWork<TInput>(Action<TInput> work) where TInput : class, new()
{
var action = new ActionBlock<TInput>(work);
Actions.Add(action);
return action;
}
public ActionBlock<TInput> AddWork<TInput>(Func<TInput, Task> work, ExecutionDataflowBlockOptions dataflowBlockOptions) where TInput : class, new()
{
var action = new ActionBlock<TInput>(work, dataflowBlockOptions);
Actions.Add(action);
return action;
}
public ActionBlock<TInput> AddWork<TInput>(Action<TInput> work, ExecutionDataflowBlockOptions dataflowBlockOptions) where TInput : class, new()
{
var action = new ActionBlock<TInput>(work, dataflowBlockOptions);
Actions.Add(action);
return action;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment