mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-07 16:45:10 +00:00
47 lines
987 B
C#
47 lines
987 B
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
#pragma warning disable CS8625
|
|
namespace TEngine
|
|
{
|
|
public sealed class FCancellationToken
|
|
{
|
|
private HashSet<Action> _actions = new HashSet<Action>();
|
|
public bool IsCancel => _actions == null;
|
|
|
|
public void Add(Action action)
|
|
{
|
|
_actions.Add(action);
|
|
}
|
|
|
|
public void Remove(Action action)
|
|
{
|
|
_actions.Remove(action);
|
|
}
|
|
|
|
public void Cancel()
|
|
{
|
|
if (_actions == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var runActions = _actions;
|
|
_actions = null;
|
|
|
|
foreach (var action in runActions)
|
|
{
|
|
try
|
|
{
|
|
action.Invoke();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Error(e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|