more refactoring
This commit is contained in:
parent
d61f46d07a
commit
886d2a88b0
13 changed files with 592 additions and 573 deletions
40
Sledgemapper/ChannelsQueue.cs
Normal file
40
Sledgemapper/ChannelsQueue.cs
Normal file
|
@ -0,0 +1,40 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading.Channels;
|
||||
|
||||
namespace Sledgemapper
|
||||
{
|
||||
public class ChannelsQueue
|
||||
{
|
||||
private readonly ChannelWriter<Action> _writer;
|
||||
|
||||
public ChannelsQueue()
|
||||
{
|
||||
var channel = Channel.CreateUnbounded<Action>(new UnboundedChannelOptions() { SingleReader = true });
|
||||
var reader = channel.Reader;
|
||||
_writer = channel.Writer;
|
||||
|
||||
Task.Run(async () =>
|
||||
{
|
||||
while (await reader.WaitToReadAsync())
|
||||
{
|
||||
// Fast loop around available jobs
|
||||
while (reader.TryRead(out var job))
|
||||
{
|
||||
job.Invoke();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void Enqueue(Action job)
|
||||
{
|
||||
_writer.TryWrite(job);
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
_writer.Complete();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue