49 lines
1.3 KiB
C#
49 lines
1.3 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using System.Threading.Channels;
|
|
using Sentry;
|
|
|
|
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))
|
|
{
|
|
try
|
|
{
|
|
job.Invoke();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
SentrySdk.CaptureException(ex);
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
public void Enqueue(Action job)
|
|
{
|
|
_writer.TryWrite(job);
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
_writer.Complete();
|
|
}
|
|
}
|
|
}
|