mirror of
https://github.com/mastercodeon314/KsDumper-11.git
synced 2024-11-24 06:50:10 +01:00
785233a68f
Updated to KDU v1.3.4 Added new Provider Selector Updated DarkControls Many bug fixes
48 lines
1.2 KiB
C#
48 lines
1.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
public class CancelableTask<T>
|
|
{
|
|
private CancellationTokenSource cancellationTokenSource;
|
|
|
|
public CancelableTask(CancellationToken cancellationToken)
|
|
{
|
|
cancellationTokenSource = new CancellationTokenSource();
|
|
cancellationToken.Register(() => cancellationTokenSource.Cancel());
|
|
}
|
|
|
|
public Task<T> CreateTask(Func<CancellationToken, T> taskFunction)
|
|
{
|
|
var taskCompletionSource = new TaskCompletionSource<T>();
|
|
|
|
Task.Run(() =>
|
|
{
|
|
try
|
|
{
|
|
T result = taskFunction(cancellationTokenSource.Token);
|
|
taskCompletionSource.TrySetResult(result);
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
taskCompletionSource.TrySetCanceled();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
taskCompletionSource.TrySetException(ex);
|
|
}
|
|
});
|
|
|
|
return taskCompletionSource.Task;
|
|
}
|
|
|
|
public void Cancel()
|
|
{
|
|
cancellationTokenSource.Cancel();
|
|
}
|
|
}
|
|
|