Files
BetterLyrics/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Helper/LatestOnlyTaskRunner.cs

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)
{
}
}
}
}