2024-09-12 20:32:54 +08:00
|
|
|
|
using Serein.Library.Api;
|
|
|
|
|
|
using Serein.Library.Utils;
|
|
|
|
|
|
using System;
|
2024-09-22 17:37:32 +08:00
|
|
|
|
using System.Security.Claims;
|
2024-09-12 20:32:54 +08:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Serein.Library.Framework.NodeFlow
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 动态流程上下文
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class DynamicContext : IDynamicContext
|
|
|
|
|
|
{
|
2024-09-24 22:39:43 +08:00
|
|
|
|
public DynamicContext(/*ISereinIOC sereinIoc,*/ IFlowEnvironment flowEnvironment)
|
2024-09-12 20:32:54 +08:00
|
|
|
|
{
|
2024-09-24 22:39:43 +08:00
|
|
|
|
// SereinIoc = sereinIoc;
|
|
|
|
|
|
Env = flowEnvironment;
|
2024-09-12 20:32:54 +08:00
|
|
|
|
}
|
2024-09-22 17:37:32 +08:00
|
|
|
|
|
2024-09-12 20:32:54 +08:00
|
|
|
|
public NodeRunCts NodeRunCts { get; set; }
|
2024-09-24 22:39:43 +08:00
|
|
|
|
// public ISereinIOC SereinIoc { get; }
|
|
|
|
|
|
public IFlowEnvironment Env { get; }
|
2024-09-22 17:37:32 +08:00
|
|
|
|
|
2024-09-12 20:32:54 +08:00
|
|
|
|
public Task CreateTimingTask(Action action, int time = 100, int count = -1)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(NodeRunCts == null)
|
|
|
|
|
|
{
|
2024-09-24 22:39:43 +08:00
|
|
|
|
NodeRunCts = Env.IOC.GetOrRegisterInstantiate<NodeRunCts>();
|
2024-09-12 20:32:54 +08:00
|
|
|
|
}
|
2024-09-21 10:06:44 +08:00
|
|
|
|
// 使用局部变量,避免捕获外部的 `action`
|
|
|
|
|
|
Action localAction = action;
|
|
|
|
|
|
|
|
|
|
|
|
return Task.Run(async () =>
|
2024-09-12 20:32:54 +08:00
|
|
|
|
{
|
2024-09-22 17:37:32 +08:00
|
|
|
|
for (int i = 0; i < count && !NodeRunCts.IsCancellationRequested; i++)
|
2024-09-12 20:32:54 +08:00
|
|
|
|
{
|
|
|
|
|
|
await Task.Delay(time);
|
2024-09-22 17:37:32 +08:00
|
|
|
|
if (NodeRunCts.IsCancellationRequested) { break; }
|
|
|
|
|
|
//if (FlowEnvironment.IsGlobalInterrupt)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// await FlowEnvironment.GetOrCreateGlobalInterruptAsync();
|
|
|
|
|
|
//}
|
2024-09-21 10:06:44 +08:00
|
|
|
|
// 确保对局部变量的引用
|
|
|
|
|
|
localAction?.Invoke();
|
2024-09-12 20:32:54 +08:00
|
|
|
|
}
|
2024-09-21 10:06:44 +08:00
|
|
|
|
|
|
|
|
|
|
// 清理引用,避免闭包导致的内存泄漏
|
|
|
|
|
|
localAction = null;
|
2024-09-12 20:32:54 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|