32 lines
902 B
C#
32 lines
902 B
C#
using System;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Threading;
|
|
|
|
namespace AnotherReplayReader.Utils
|
|
{
|
|
internal class TaskQueue
|
|
{
|
|
private readonly object _lock = new();
|
|
private readonly Dispatcher _dispatcher;
|
|
private Task _current = Task.CompletedTask;
|
|
|
|
public TaskQueue(Dispatcher dispatcher)
|
|
{
|
|
_dispatcher = dispatcher;
|
|
}
|
|
|
|
public Task Enqueue(Func<Task> getTask, CancellationToken cancelToken)
|
|
{
|
|
using var locker = new Lock(_lock);
|
|
_current = _current.ContinueWith(async t =>
|
|
{
|
|
var task = await _dispatcher.InvokeAsync(getTask, DispatcherPriority.Background, cancelToken);
|
|
await task.ConfigureAwait(false);
|
|
}, cancelToken).Unwrap();
|
|
return _current.IgnoreCancel();
|
|
}
|
|
|
|
}
|
|
}
|