1
0
mirror of https://github.com/chylex/Discord-History-Tracker.git synced 2025-04-05 11:15:42 +02:00
Discord-History-Tracker/app/Utils/Tasks/CancellableTask.cs
2025-03-17 17:27:12 +01:00

29 lines
699 B
C#

using System;
using System.Threading;
using System.Threading.Tasks;
namespace DHT.Utils.Tasks;
public sealed class CancellableTask : IDisposable {
public static CancellableTask Run(Func<CancellationToken, Task> action) {
return new CancellableTask(action);
}
public Task Task { get; }
private readonly CancellationTokenSource cancellationTokenSource = new ();
private CancellableTask(Func<CancellationToken, Task> action) {
CancellationToken cancellationToken = cancellationTokenSource.Token;
Task = Task.Run(() => action(cancellationToken));
}
public void Cancel() {
cancellationTokenSource.Cancel();
}
public void Dispose() {
cancellationTokenSource.Dispose();
}
}