Last active
May 16, 2022 14:30
-
-
Save markrendle/0d5b8bee603c16d224af48490d135569 to your computer and use it in GitHub Desktop.
Better implementation of ChannelsQueue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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