Better implementation of ChannelsQueue
public class ChannelsQueue : IJobQueue<Action> | |
{ | |
private ChannelWriter<Action> _writer; | |
public ChannelsQueue() | |
{ | |
var channel = Channel.CreateUnbounded<Action>(); | |
var reader = channel.Reader; | |
_writer = channel.Writer; | |
Task.Factory.StartNew(async () => | |
{ | |
while (await reader.WaitToReadAsync()) | |
{ | |
// Fast loop around available jobs | |
while (reader.TryRead(out var job)) | |
{ | |
job.Invoke(); | |
} | |
} | |
} , TaskCreationOptions.LongRunning); | |
} | |
public async void Enqueue(Action job) | |
{ | |
_writer.TryWrite(job); | |
} | |
public void Stop() | |
{ | |
_writer.Complete(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment