改了一亿个东西,修了一亿个 BUG
This commit is contained in:
30
Utils/TaskQueue.cs
Normal file
30
Utils/TaskQueue.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
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 =>
|
||||
{
|
||||
await _dispatcher.InvokeAsync(getTask, DispatcherPriority.Background, cancelToken);
|
||||
}, cancelToken);
|
||||
return _current.IgnoreCancel();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user