mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
[+] 接入ET8服务端
[+] 接入ET8服务端
This commit is contained in:
54
Assets/GameScripts/ThirdParty/ETTask/ETCancellationToken.cs
vendored
Normal file
54
Assets/GameScripts/ThirdParty/ETTask/ETCancellationToken.cs
vendored
Normal file
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace ET
|
||||
{
|
||||
public class ETCancellationToken
|
||||
{
|
||||
private HashSet<Action> actions = new HashSet<Action>();
|
||||
|
||||
public void Add(Action callback)
|
||||
{
|
||||
// 如果action是null,绝对不能添加,要抛异常,说明有协程泄漏
|
||||
this.actions.Add(callback);
|
||||
}
|
||||
|
||||
public void Remove(Action callback)
|
||||
{
|
||||
this.actions?.Remove(callback);
|
||||
}
|
||||
|
||||
public bool IsDispose()
|
||||
{
|
||||
return this.actions == null;
|
||||
}
|
||||
|
||||
public void Cancel()
|
||||
{
|
||||
if (this.actions == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this.Invoke();
|
||||
}
|
||||
|
||||
private void Invoke()
|
||||
{
|
||||
HashSet<Action> runActions = this.actions;
|
||||
this.actions = null;
|
||||
try
|
||||
{
|
||||
foreach (Action action in runActions)
|
||||
{
|
||||
action.Invoke();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ETTask.ExceptionHandler.Invoke(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user