mirror of
https://github.com/jayfunc/BetterLyrics.git
synced 2026-01-13 03:34:55 +08:00
35 lines
745 B
C#
35 lines
745 B
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace BetterLyrics.WinUI3.Helper
|
|
{
|
|
public class LatestOnlyTaskRunner
|
|
{
|
|
private CancellationTokenSource? _cts;
|
|
|
|
public async Task RunAsync(Func<CancellationToken, Task> taskFactory)
|
|
{
|
|
_cts?.Cancel();
|
|
_cts?.Dispose();
|
|
|
|
_cts = new CancellationTokenSource();
|
|
var token = _cts.Token;
|
|
|
|
try
|
|
{
|
|
await taskFactory(token);
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
}
|
|
}
|
|
}
|