mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
feat(coroutine): 添加基于条件和取消令牌的重复调用功能
- 新增RepeatCallForever方法支持条件函数参数 - 添加CancellationToken参数支持取消令牌控制 - 重构原有RepeatCallForever方法保持向后兼容 - 添加详细的XML文档注释说明参数和返回值 - 实现基于条件判断的循环执行逻辑 - 实现基于取消令牌的循环执行控制
This commit is contained in:
parent
bf517a4f7f
commit
7314bf03d0
@ -83,16 +83,42 @@ public static class CoroutineHelper
|
||||
yield return new Delay(interval);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 无限重复调用指定的委托
|
||||
/// 无限重复调用指定的委托,直到条件不满足
|
||||
/// </summary>
|
||||
/// <param name="interval">每次调用之间的间隔时间(秒)</param>
|
||||
/// <param name="action">要执行的动作委托</param>
|
||||
/// <param name="shouldContinue">继续执行的条件,返回false时停止</param>
|
||||
/// <returns>返回一个枚举器,用于协程执行</returns>
|
||||
public static IEnumerator<IYieldInstruction> RepeatCallForever(double interval, Action? action)
|
||||
public static IEnumerator<IYieldInstruction> RepeatCallForever(
|
||||
double interval,
|
||||
Action? action,
|
||||
Func<bool>? shouldContinue = null)
|
||||
{
|
||||
while (true)
|
||||
// 循环执行动作直到条件不再满足
|
||||
while (shouldContinue?.Invoke() ?? true)
|
||||
{
|
||||
action?.Invoke();
|
||||
yield return new Delay(interval);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 无限重复调用指定的委托,直到取消令牌被触发
|
||||
/// </summary>
|
||||
/// <param name="interval">每次调用之间的间隔时间(秒)</param>
|
||||
/// <param name="action">要执行的动作委托</param>
|
||||
/// <param name="token">用于控制循环执行的取消令牌</param>
|
||||
/// <returns>返回一个枚举器,用于协程执行</returns>
|
||||
public static IEnumerator<IYieldInstruction> RepeatCallForever(
|
||||
double interval,
|
||||
Action? action,
|
||||
CancellationToken token
|
||||
)
|
||||
{
|
||||
// 循环执行动作直到取消令牌被请求取消
|
||||
while (!token.IsCancellationRequested)
|
||||
{
|
||||
action?.Invoke();
|
||||
yield return new Delay(interval);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user