From 3537a49784512c33df36d7d18e1c58cdce1232b1 Mon Sep 17 00:00:00 2001 From: fengjiayi <12821976+ning_xi@user.noreply.gitee.com> Date: Sat, 21 Sep 2024 10:06:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E4=BA=86=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E8=BE=93=E5=87=BA=EF=BC=8C=E6=9B=B4=E6=94=B9=E4=BA=86ChannelFl?= =?UTF-8?q?owTrigger=E5=AD=98=E5=9C=A8=E7=9A=84=E5=86=85=E5=AD=98=E6=B3=84?= =?UTF-8?q?=E6=BC=8F=EF=BC=88=E5=8F=96=E6=B6=88=E8=B6=85=E6=97=B6=E6=9C=BA?= =?UTF-8?q?=E5=88=B6=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Library.Framework/NodeFlow/DynamicContext.cs | 13 +- Library/Api/IDynamicContext.cs | 1 - Library/Api/ISereinIoc.cs | 6 + Library/Utils/ChannelFlowInterrupt.cs | 10 +- Library/Utils/ChannelFlowTrigger.cs | 55 +++-- Library/Utils/SereinIoc.cs | 58 ++++- NodeFlow/Base/NodeModelBaseFunc.cs | 226 ++++++++----------- NodeFlow/FlowEnvironment.cs | 16 +- NodeFlow/FlowStarter.cs | 205 +++++++++++------ NodeFlow/Model/SingleActionNode.cs | 12 +- NodeFlow/Model/SingleFlipflopNode.cs | 40 +++- WorkBench/LogWindow.xaml.cs | 100 +++++++- WorkBench/MainWindow.xaml.cs | 123 ++++++++-- WorkBench/Node/NodeControlViewModelBase.cs | 2 +- WorkBench/tool/LogTextWriter.cs | 61 +++-- 15 files changed, 624 insertions(+), 304 deletions(-) diff --git a/Library.Framework/NodeFlow/DynamicContext.cs b/Library.Framework/NodeFlow/DynamicContext.cs index deb4fae..f245204 100644 --- a/Library.Framework/NodeFlow/DynamicContext.cs +++ b/Library.Framework/NodeFlow/DynamicContext.cs @@ -24,17 +24,24 @@ namespace Serein.Library.Framework.NodeFlow { if(NodeRunCts == null) { - NodeRunCts = SereinIoc.GetOrRegisterInstantiate(); } - return Task.Factory.StartNew(async () => + // 使用局部变量,避免捕获外部的 `action` + Action localAction = action; + + return Task.Run(async () => { for (int i = 0; i < count; i++) { NodeRunCts.Token.ThrowIfCancellationRequested(); await Task.Delay(time); - action.Invoke(); + + // 确保对局部变量的引用 + localAction?.Invoke(); } + + // 清理引用,避免闭包导致的内存泄漏 + localAction = null; }); } } diff --git a/Library/Api/IDynamicContext.cs b/Library/Api/IDynamicContext.cs index d6bcc4a..10e1158 100644 --- a/Library/Api/IDynamicContext.cs +++ b/Library/Api/IDynamicContext.cs @@ -8,7 +8,6 @@ namespace Serein.Library.Api { IFlowEnvironment FlowEnvironment { get; } ISereinIOC SereinIoc { get; } - NodeRunCts NodeRunCts { get; set; } Task CreateTimingTask(Action action, int time = 100, int count = -1); } } diff --git a/Library/Api/ISereinIoc.cs b/Library/Api/ISereinIoc.cs index a5be469..83c3f9d 100644 --- a/Library/Api/ISereinIoc.cs +++ b/Library/Api/ISereinIoc.cs @@ -25,6 +25,12 @@ namespace Serein.Library.Api /// 获取或创建并注入目标类型 /// object GetOrRegisterInstantiate(Type type); + object Get(Type type); + + + T Get(string name); + object Get(string name); + void CustomRegisterInstance(string name, object instance, bool needInjectProperty = true); /// /// 创建目标类型的对象, 并注入依赖项 diff --git a/Library/Utils/ChannelFlowInterrupt.cs b/Library/Utils/ChannelFlowInterrupt.cs index 1391444..22ced6c 100644 --- a/Library/Utils/ChannelFlowInterrupt.cs +++ b/Library/Utils/ChannelFlowInterrupt.cs @@ -37,12 +37,20 @@ namespace Serein.Library.Utils try { await Task.Delay(outTime, cts.Token); - await channel.Writer.WriteAsync(CancelType.Overtime); + if(!cts.Token.IsCancellationRequested) + { + await channel.Writer.WriteAsync(CancelType.Overtime); + } + } catch (OperationCanceledException) { // 超时任务被取消 } + finally + { + cts?.Dispose(); + } }, cts.Token); // 等待信号传入(超时或手动触发) diff --git a/Library/Utils/ChannelFlowTrigger.cs b/Library/Utils/ChannelFlowTrigger.cs index 4ec726d..eccddd1 100644 --- a/Library/Utils/ChannelFlowTrigger.cs +++ b/Library/Utils/ChannelFlowTrigger.cs @@ -34,8 +34,11 @@ namespace Serein.Library.NodeFlow.Tool // 使用并发字典管理每个枚举信号对应的 Channel private readonly ConcurrentDictionary> _channels = new ConcurrentDictionary>(); + + // 到期后自动触发。短时间内触发频率过高的情况下,请将 outTime 设置位短一些的时间,因为如果超时等待时间过长,会导致非预期的“托管内存泄露”。 + /// - /// 创建信号并指定超时时间,到期后自动触发(异步方法) + /// 创建信号并指定超时时间的Channel. /// /// 枚举信号标识符 /// 超时时间 @@ -43,29 +46,39 @@ namespace Serein.Library.NodeFlow.Tool public async Task CreateChannelWithTimeoutAsync(TSignal signal, TimeSpan outTime, TResult outValue) { var channel = GetOrCreateChannel(signal); - var cts = new CancellationTokenSource(); - - // 异步任务:超时后自动触发信号 - _ = Task.Run(async () => - { - try - { - await Task.Delay(outTime, cts.Token); - TriggerData triggerData = new TriggerData() - { - Value = outValue, - Type = TriggerType.Overtime, - }; - await channel.Writer.WriteAsync(triggerData); - } - catch (OperationCanceledException) - { - // 超时任务被取消 - } - }, cts.Token); + //var cts = new CancellationTokenSource(); + //// 异步任务:超时后自动触发信号 + //_ = Task.Run(async () => + //{ + // try + // { + // await Task.Delay(outTime, cts.Token); + // if(!cts.IsCancellationRequested) // 如果还没有被取消 + // { + // TriggerData triggerData = new TriggerData() + // { + // Value = outValue, + // Type = TriggerType.Overtime, + // }; + // await channel.Writer.WriteAsync(triggerData); + // } + // } + // catch (OperationCanceledException) + // { + // // 超时任务被取消 + // } + // finally + // { + // cts?.Cancel(); + // cts?.Dispose(); // 确保 cts 被释放 + // } + //}, cts.Token); + // 等待信号传入(超时或手动触发) var result = await channel.Reader.ReadAsync(); + //cts?.Cancel(); + //cts?.Dispose(); return result; } diff --git a/Library/Utils/SereinIoc.cs b/Library/Utils/SereinIoc.cs index 047d391..03c238a 100644 --- a/Library/Utils/SereinIoc.cs +++ b/Library/Utils/SereinIoc.cs @@ -1,4 +1,5 @@ -using Serein.Library.Api; +using Newtonsoft.Json.Linq; +using Serein.Library.Api; using Serein.Library.Attributes; using Serein.Library.Web; using System; @@ -6,6 +7,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Reflection; +using System.Xml; namespace Serein.Library.Utils { @@ -88,8 +90,7 @@ namespace Serein.Library.Utils public ISereinIOC Register(params object[] parameters) where TImplementation : TService { - var typeFullName = typeof(TService).FullName; - RegisterType(typeFullName, typeof(TImplementation)); + RegisterType(typeof(TService).FullName, typeof(TImplementation)); return this; } #endregion @@ -140,6 +141,57 @@ namespace Serein.Library.Utils return (T)GetOrRegisterInstantiate(typeof(T)); } + + + public void CustomRegisterInstance(string name,object instance, bool needInjectProperty = true) + { + // 不存在时才允许创建 + if (!_dependencies.ContainsKey(name)) + { + _dependencies.TryAdd(name, instance); + } + + if (needInjectProperty) + { + InjectDependencies(instance); // 注入实例需要的依赖项 + } + + // 检查是否存在其它实例 + if (_unfinishedDependencies.TryGetValue(name, out var unfinishedPropertyList)) + { + foreach ((object obj, PropertyInfo property) in unfinishedPropertyList) + { + property.SetValue(obj, instance); //注入依赖项 + } + + if (_unfinishedDependencies.TryRemove(name, out unfinishedPropertyList)) + { + unfinishedPropertyList.Clear(); + } + } + } + public object Get(Type type) + { + return Get(type.FullName); + } + + + public T Get(string name) + { + return (T)Get(name); + } + public object Get(string name) + { + object value; + if (!_dependencies.TryGetValue(name, out value)) + { + value = null; + } + return value; + } + + + /// /// 根据类型生成对应的实例,并注入其中的依赖项(类型信息不登记到IOC容器中),类型创建后自动注入其它需要此类型的对象 /// diff --git a/NodeFlow/Base/NodeModelBaseFunc.cs b/NodeFlow/Base/NodeModelBaseFunc.cs index 1c3059a..208e698 100644 --- a/NodeFlow/Base/NodeModelBaseFunc.cs +++ b/NodeFlow/Base/NodeModelBaseFunc.cs @@ -11,6 +11,7 @@ using System.Linq; using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; +using System.Xml.Linq; using static Serein.Library.Utils.ChannelFlowInterrupt; namespace Serein.NodeFlow.Base @@ -35,6 +36,7 @@ namespace Serein.NodeFlow.Base this.DebugSetting.InterruptClass = InterruptClass.Branch; this.DebugSetting.IsInterrupt = true; } + /// /// 不再中断 /// @@ -90,22 +92,6 @@ namespace Serein.NodeFlow.Base node.MethodDetails.ExplicitDatas[i].DataValue = pd.Value; } } - - //if (control is ConditionNodeControl conditionNodeControl) - //{ - // conditionNodeControl.ViewModel.IsCustomData = pd.state; - // conditionNodeControl.ViewModel.CustomData = pd.value; - // conditionNodeControl.ViewModel.Expression = pd.expression; - //} - //else if (control is ExpOpNodeControl expOpNodeControl) - //{ - // expOpNodeControl.ViewModel.Expression = pd.expression; - //} - //else - //{ - // node.MethodDetails.ExplicitDatas[i].IsExplicitData = pd.state; - // node.MethodDetails.ExplicitDatas[i].DataValue = pd.value; - //} return this; } @@ -120,101 +106,85 @@ namespace Serein.NodeFlow.Base /// public async Task StartExecute(IDynamicContext context) { - var cts = context.SereinIoc.GetOrRegisterInstantiate(); + CancellationTokenSource cts = null; - Stack stack = new Stack(); - stack.Push(this); - while (stack.Count > 0 && !cts.IsCancellationRequested) // 循环中直到栈为空才会退出循环 + try { - // 从栈中弹出一个节点作为当前节点进行处理 - var currentNode = stack.Pop(); + cts = context.SereinIoc.Get(FlowStarter.FlipFlopCtsName); - // 设置方法执行的对象 - if (currentNode.MethodDetails?.ActingInstance == null && currentNode.MethodDetails?.ActingInstanceType is not null) + + Stack stack = new Stack(); + stack.Push(this); + while (stack.Count > 0 && !cts.IsCancellationRequested) // 循环中直到栈为空才会退出循环 { - currentNode.MethodDetails.ActingInstance ??= context.SereinIoc.GetOrRegisterInstantiate(currentNode.MethodDetails.ActingInstanceType); - } + // 从栈中弹出一个节点作为当前节点进行处理 + var currentNode = stack.Pop(); - //if (TryCreateInterruptTask(context, currentNode, out Task? task)) - //{ - // var cancelType = await task!; - // await Console.Out.WriteLineAsync($"[{currentNode.MethodDetails.MethodName}]中断已{(cancelType == CancelType.Manual ? "手动取消" : "自动取消")},开始执行后继分支"); - //} - - #region 执行相关 - // 首先执行上游分支 - var upstreamNodes = currentNode.SuccessorNodes[ConnectionType.Upstream]; - for (int i = upstreamNodes.Count - 1; i >= 0; i--) - { - if (upstreamNodes[i].DebugSetting.IsEnable) // 排除未启用的上游节点 + // 设置方法执行的对象 + if (currentNode.MethodDetails?.ActingInstance == null && currentNode.MethodDetails?.ActingInstanceType is not null) { - upstreamNodes[i].PreviousNode = currentNode; - await upstreamNodes[i].StartExecute(context); // 执行流程节点的上游分支 + currentNode.MethodDetails.ActingInstance ??= context.SereinIoc.GetOrRegisterInstantiate(currentNode.MethodDetails.ActingInstanceType); } - } - currentNode.FlowData = currentNode.ExecutingAsync(context); // 流程中正常执行 - - // 判断是否为触发器节点,如果是,则开始等待。 - //if (currentNode.MethodDetails != null && currentNode.MethodDetails.MethodDynamicType == NodeType.Flipflop) - //{ - - // currentNode.FlowData = await currentNode.ExecutingFlipflopAsync(context); // 流程中遇到了触发器 - //} - //else - //{ - - //} - #endregion - - - - - #region 执行完成 - if (currentNode.NextOrientation == ConnectionType.None) break; // 不再执行 - - - // 选择后继分支 - var nextNodes = currentNode.SuccessorNodes[currentNode.NextOrientation]; - - // 将下一个节点集合中的所有节点逆序推入栈中 - for (int i = nextNodes.Count - 1; i >= 0; i--) - { - // 排除未启用的节点 - if (nextNodes[i].DebugSetting.IsEnable) + #region 执行相关 + // 首先执行上游分支 +#if false + var upstreamNodes = currentNode.SuccessorNodes[ConnectionType.Upstream]; + for (int i = upstreamNodes.Count - 1; i >= 0; i--) { - nextNodes[i].PreviousNode = currentNode; - stack.Push(nextNodes[i]); + if (upstreamNodes[i].DebugSetting.IsEnable) // 排除未启用的上游节点 + { + upstreamNodes[i].PreviousNode = currentNode; + await upstreamNodes[i].StartExecute(context); // 执行流程节点的上游分支 + } + } +#endif + + currentNode.FlowData = await currentNode.ExecutingAsync(context); // 流程中正常执行 + #endregion + + #region 执行完成 + if (currentNode.NextOrientation == ConnectionType.None) break; // 不再执行 + + + // 选择后继分支 + var nextNodes = currentNode.SuccessorNodes[currentNode.NextOrientation]; + + // 将下一个节点集合中的所有节点逆序推入栈中 + for (int i = nextNodes.Count - 1; i >= 0; i--) + { + // 排除未启用的节点 + if (nextNodes[i].DebugSetting.IsEnable) + { + nextNodes[i].PreviousNode = currentNode; + stack.Push(nextNodes[i]); + } } - } - #endregion + #endregion + } + } + finally + { + cts?.Dispose(); } } public static bool TryCreateInterruptTask(IDynamicContext context, NodeModelBase currentNode, out Task? task) { - if (!currentNode.DebugSetting.IsInterrupt) - { - task = null; - return false; - } - - Task? result = null; - bool haveTask = false; + bool haveTask; Console.WriteLine($"[{currentNode.MethodDetails.MethodName}]在当前分支中断"); + if (currentNode.DebugSetting.InterruptClass == InterruptClass.None) { haveTask = false; task = null; currentNode.DebugSetting.IsInterrupt = false; // 纠正设置 } - if (currentNode.DebugSetting.InterruptClass == InterruptClass.Branch) // 中断当前分支 + else if (currentNode.DebugSetting.InterruptClass == InterruptClass.Branch) // 中断当前分支 { currentNode.DebugSetting.IsInterrupt = true; haveTask = true; - task = context.FlowEnvironment.ChannelFlowInterrupt.CreateChannelWithTimeoutAsync(currentNode.Guid, TimeSpan.FromSeconds(1)); - currentNode.CancelInterruptCallback ??= () => context.FlowEnvironment.ChannelFlowInterrupt.TriggerSignal(currentNode.Guid); - + task = context.FlowEnvironment.ChannelFlowInterrupt.CreateChannelWithTimeoutAsync(currentNode.Guid, TimeSpan.FromSeconds(60 * 30)); // 中断30分钟 } else { @@ -233,16 +203,19 @@ namespace Serein.NodeFlow.Base public virtual async Task ExecutingAsync(IDynamicContext context) { #region 调试中断 - if (TryCreateInterruptTask(context, this, out Task? task)) + if (DebugSetting.IsInterrupt && TryCreateInterruptTask(context, this, out Task? task)) // 执行节点前检查中断 { + string guid = this.Guid.ToString(); + this.CancelInterruptCallback ??= () => context.FlowEnvironment.ChannelFlowInterrupt.TriggerSignal(guid); var cancelType = await task!; + task?.ToString(); await Console.Out.WriteLineAsync($"[{this.MethodDetails.MethodName}]中断已{(cancelType == CancelType.Manual ? "手动取消" : "自动取消")},开始执行后继分支"); } #endregion MethodDetails md = MethodDetails; - var del = md.MethodDelegate; + var del = md.MethodDelegate.Clone(); object instance = md.ActingInstance; var haveParameter = md.ExplicitDatas.Length > 0; @@ -250,13 +223,42 @@ namespace Serein.NodeFlow.Base try { // Action/Func([方法作用的实例],[可能的参数值],[可能的返回值]) + object?[]? parameters = GetParameters(context, md); object? result = (haveParameter, haveResult) switch { (false, false) => Execution((Action)del, instance), // 调用节点方法,返回null - (true, false) => Execution((Action)del, instance, GetParameters(context, md)), // 调用节点方法,返回null + (true, false) => Execution((Action)del, instance, parameters), // 调用节点方法,返回null (false, true) => Execution((Func)del, instance), // 调用节点方法,返回方法传回类型 - (true, true) => Execution((Func)del, instance, GetParameters(context, md)), // 调用节点方法,获取入参参数,返回方法忏悔类型 + (true, true) => Execution((Func)del, instance, parameters), // 调用节点方法,获取入参参数,返回方法忏悔类型 }; + + //object?[]? parameters; + //object? result = null; + //if ( haveParameter ) + //{ + // var data = GetParameters(context, md); + + // if (data[0] is Int32 count && count > 1) + // { + // } + // parameters = [instance, data]; + //} + //else + //{ + // parameters = [instance]; + //} + + //if (haveResult) + //{ + // result = del.DynamicInvoke(parameters); + //} + //else + //{ + // del.DynamicInvoke(parameters); + //} + + + NextOrientation = ConnectionType.IsSucceed; return result; } @@ -307,7 +309,7 @@ namespace Serein.NodeFlow.Base // 用正确的大小初始化参数数组 if (md.ExplicitDatas.Length == 0) { - return [];// md.ActingInstance + return null;// md.ActingInstance } object?[]? parameters = new object[md.ExplicitDatas.Length]; @@ -344,7 +346,7 @@ namespace Serein.NodeFlow.Base { parameters[i] = ed.DataType switch { - Type t when t == previousDataType => context, // 上下文 + //Type t when t == previousDataType => inputParameter, // 上下文 Type t when t == typeof(IDynamicContext) => context, // 上下文 Type t when t == typeof(MethodDetails) => md, // 节点方法描述 Type t when t == typeof(NodeModelBase) => this, // 节点实体类 @@ -387,45 +389,5 @@ namespace Serein.NodeFlow.Base #endregion - - - /// - /// json文本反序列化为对象 - /// - /// - /// - /// - private dynamic? ConvertValue(string value, Type targetType) - { - try - { - if (!string.IsNullOrEmpty(value)) - { - return JsonConvert.DeserializeObject(value, targetType); - } - else - { - return null; - } - } - catch (JsonReaderException ex) - { - Console.WriteLine(ex); - return value; - } - catch (JsonSerializationException ex) - { - // 如果无法转为对应的JSON对象 - int startIndex = ex.Message.IndexOf("to type '") + "to type '".Length; // 查找类型信息开始的索引 - int endIndex = ex.Message.IndexOf('\''); // 查找类型信息结束的索引 - var typeInfo = ex.Message[startIndex..endIndex]; // 提取出错类型信息,该怎么传出去? - Console.WriteLine("无法转为对应的JSON对象:" + typeInfo); - return null; - } - catch // (Exception ex) - { - return value; - } - } } } diff --git a/NodeFlow/FlowEnvironment.cs b/NodeFlow/FlowEnvironment.cs index 3fa7bcc..0943fe0 100644 --- a/NodeFlow/FlowEnvironment.cs +++ b/NodeFlow/FlowEnvironment.cs @@ -9,6 +9,7 @@ using Serein.NodeFlow.Model; using Serein.NodeFlow.Tool; using System.Collections.Concurrent; using System.Reflection; +using System.Xml.Linq; using static Serein.NodeFlow.FlowStarter; namespace Serein.NodeFlow @@ -196,9 +197,20 @@ namespace Serein.NodeFlow } public void Exit() { + foreach (var node in Nodes.Values) + { + if (typeof(IDisposable).IsAssignableFrom(node?.FlowData?.GetType()) && node.FlowData is IDisposable disposable) + { + disposable?.Dispose(); + } + node!.FlowData = null; + } + ChannelFlowInterrupt?.CancelAllTasks(); flowStarter?.Exit(); OnFlowRunComplete?.Invoke(new FlowEventArgs()); + + GC.Collect(); } /// @@ -452,7 +464,9 @@ namespace Serein.NodeFlow for (int i = 0; i < pnc.Value.Count; i++) { NodeModelBase? pNode = pnc.Value[i]; - pNode.SuccessorNodes[pCType].RemoveAt(i); + //pNode.SuccessorNodes[pCType].RemoveAt(i); + pNode.SuccessorNodes[pCType].Remove(pNode); + OnNodeConnectChange?.Invoke(new NodeConnectChangeEventArgs(pNode.Guid, remoteNode.Guid, pCType, diff --git a/NodeFlow/FlowStarter.cs b/NodeFlow/FlowStarter.cs index e57d446..7155807 100644 --- a/NodeFlow/FlowStarter.cs +++ b/NodeFlow/FlowStarter.cs @@ -7,6 +7,7 @@ using Serein.Library.Web; using Serein.NodeFlow.Base; using Serein.NodeFlow.Model; using System.ComponentModel.Design; +using System.Runtime.CompilerServices; using static Serein.Library.Utils.ChannelFlowInterrupt; namespace Serein.NodeFlow @@ -43,10 +44,12 @@ namespace Serein.NodeFlow Completion, } /// - /// 控制触发器的结束 + /// 控制触发器 /// - private NodeRunCts FlipFlopCts { get; set; } = null; + + public const string FlipFlopCtsName = "<>.FlowFlipFlopCts"; + public bool IsStopStart = false; /// /// 运行状态 /// @@ -68,6 +71,16 @@ namespace Serein.NodeFlow /// private IDynamicContext Context { get; set; } = null; + private void CheckStartState() + { + if (IsStopStart) + { + throw new Exception("停止启动"); + + } + } + + /// /// 开始运行 /// @@ -95,14 +108,14 @@ namespace Serein.NodeFlow #region 选择运行环境的上下文 // 判断使用哪一种流程上下文 - var isNetFramework = true; + var isNetFramework = false; if (isNetFramework) { Context = new Serein.Library.Framework.NodeFlow.DynamicContext(SereinIOC, env); } else { - Context = new Serein.Library.Core.NodeFlow.DynamicContext(SereinIOC, env); + Context = new Serein.Library.Core.NodeFlow.DynamicContext(SereinIOC, env); // 从起始节点启动流程时创建上下文 } #endregion @@ -119,24 +132,36 @@ namespace Serein.NodeFlow { nodeMd.ActingInstance = null; } - SereinIOC.Reset(); // 开始运行时清空ioc中注册的实例 // 初始化ioc容器中的类型对象 foreach (var md in thisRuningMds) { - if(md.ActingInstanceType != null) + if (md.ActingInstanceType != null) { SereinIOC.Register(md.ActingInstanceType); } - } - SereinIOC.Build(); // 流程启动前的初始化 - foreach (var md in thisRuningMds) - { - if (md.ActingInstanceType != null) + else { - md.ActingInstance = SereinIOC.GetOrRegisterInstantiate(md.ActingInstanceType); + await Console.Out.WriteLineAsync($"{md.MethodName} - 没有类型声明"); + IsStopStart = true; } } + CheckStartState(); + + + SereinIOC.Build(); // 流程启动前的初始化 + + foreach (var md in thisRuningMds) + { + md.ActingInstance = SereinIOC.GetOrRegisterInstantiate(md.ActingInstanceType); + if(md.ActingInstance is null) + { + await Console.Out.WriteLineAsync($"{md.MethodName} - 无法获取类型[{md.ActingInstanceType}]的实例"); + IsStopStart = true; + } + } + + CheckStartState(); //foreach (var md in flipflopNodes.Select(it => it.MethodDetails).ToArray()) //{ @@ -144,34 +169,34 @@ namespace Serein.NodeFlow //} #endregion - #region 检查并修正初始化、加载时、退出时方法作用的对象,保证后续不会报错 - foreach (var md in initMethods) // 初始化 - { - md.ActingInstance ??= Context.SereinIoc.GetOrRegisterInstantiate(md.ActingInstanceType); - } - foreach (var md in loadingMethods) // 加载 - { - md.ActingInstance ??= Context.SereinIoc.GetOrRegisterInstantiate(md.ActingInstanceType); - } - foreach (var md in exitMethods) // 初始化 - { - md.ActingInstance ??= Context.SereinIoc.GetOrRegisterInstantiate(md.ActingInstanceType); - } + #region 检查并修正初始化、加载时、退出时方法作用的对象,保证后续不会报错(已注释) + //foreach (var md in initMethods) // 初始化 + //{ + // md.ActingInstance ??= Context.SereinIoc.GetOrRegisterInstantiate(md.ActingInstanceType); + //} + //foreach (var md in loadingMethods) // 加载 + //{ + // md.ActingInstance ??= Context.SereinIoc.GetOrRegisterInstantiate(md.ActingInstanceType); + //} + //foreach (var md in exitMethods) // 初始化 + //{ + // md.ActingInstance ??= Context.SereinIoc.GetOrRegisterInstantiate(md.ActingInstanceType); + //} #endregion #region 执行初始化,绑定IOC容器,再执行加载时 - object?[]? args = [Context]; + //object?[]? args = [Context]; foreach (var md in initMethods) // 初始化 { - object?[]? data = [md.ActingInstance, args]; - md.MethodDelegate.DynamicInvoke(data); + ((Action)md.MethodDelegate).Invoke(md.ActingInstance, [Context]); } Context.SereinIoc.Build(); // 绑定初始化时注册的类型 foreach (var md in loadingMethods) // 加载 { - object?[]? data = [md.ActingInstance, args]; - md.MethodDelegate.DynamicInvoke(data); + //object?[]? data = [md.ActingInstance, args]; + //md.MethodDelegate.DynamicInvoke(data); + ((Action)md.MethodDelegate).Invoke(md.ActingInstance, [Context]); } Context.SereinIoc.Build(); // 预防有人在加载时才注册类型,再绑定一次 #endregion @@ -179,38 +204,43 @@ namespace Serein.NodeFlow #region 设置流程退出时的回调函数 ExitAction = () => { + SereinIOC.Run(web => { web?.Stop(); }); foreach (MethodDetails? md in exitMethods) { - object?[]? data = [md.ActingInstance, args]; - md.MethodDelegate.DynamicInvoke(data); - } - if (Context != null && Context.NodeRunCts != null && !Context.NodeRunCts.IsCancellationRequested) - { - Context.NodeRunCts.Cancel(); - } - if (FlipFlopCts != null && !FlipFlopCts.IsCancellationRequested) - { - FlipFlopCts.Cancel(); + ((Action)md.MethodDelegate).Invoke(md.ActingInstance, [Context]); } + + //if (Context != null && Context.NodeRunCts != null && !Context.NodeRunCts.IsCancellationRequested) + //{ + // Context.NodeRunCts.Cancel(); + //} + + //if (FlipFlopCts != null && !FlipFlopCts.IsCancellationRequested) + //{ + // FlipFlopCts?.Cancel(); + // FlipFlopCts?.Dispose(); + //} FlowState = RunState.Completion; FlipFlopState = RunState.Completion; }; #endregion #region 开始启动流程 - + CancellationTokenSource FlipFlopCts = null; try { - + if (flipflopNodes.Count > 0) { FlipFlopState = RunState.Running; // 如果存在需要启动的触发器,则开始启动 - FlipFlopCts = SereinIOC.GetOrRegisterInstantiate(); + FlipFlopCts = new CancellationTokenSource(); + SereinIOC.CustomRegisterInstance(FlipFlopCtsName, FlipFlopCts,false); + // 使用 TaskCompletionSource 创建未启动的触发器任务 var tasks = flipflopNodes.Select(async node => { @@ -220,18 +250,25 @@ namespace Serein.NodeFlow } await startNode.StartExecute(Context); // 开始运行时从起始节点开始运行 // 等待结束 - if (FlipFlopCts != null) + if(FlipFlopState == RunState.Running && FlipFlopCts is not null) { while (!FlipFlopCts.IsCancellationRequested) { await Task.Delay(100); } } + + //FlipFlopCts?.Dispose(); } catch (Exception ex) { await Console.Out.WriteLineAsync(ex.ToString()); - } + } + finally + { + FlipFlopCts?.Dispose(); + FlowState = RunState.Completion; + } #endregion } @@ -253,36 +290,70 @@ namespace Serein.NodeFlow /// private async Task FlipflopExecute(IFlowEnvironment flowEnvironment,SingleFlipflopNode singleFlipFlopNode) { - var context = new DynamicContext(SereinIOC, flowEnvironment); + CancellationTokenSource cts = null; + + var context = new DynamicContext(SereinIOC, flowEnvironment); // 启动全局触发器时新建上下文 + MethodDetails md = singleFlipFlopNode.MethodDetails; var del = md.MethodDelegate; - - // 设置方法执行的对象 - if (md?.ActingInstance == null && md?.ActingInstanceType is not null) - { - md.ActingInstance ??= context.SereinIoc.GetOrRegisterInstantiate(md.ActingInstanceType); - } object?[]? parameters = singleFlipFlopNode.GetParameters(context, singleFlipFlopNode.MethodDetails); // 启动全局触发器时获取入参参数 // 设置委托对象 var func = md.ExplicitDatas.Length == 0 ? (Func>)del : (Func>)del; + + bool t = md.ExplicitDatas.Length == 0; try { - while (!FlipFlopCts.IsCancellationRequested) + + + cts = Context.SereinIoc.Get(FlipFlopCtsName); + + while (!cts.IsCancellationRequested) { - IFlipflopContext flipflopContext = await func.Invoke(md.ActingInstance, parameters);// 开始等待全局触发器的触发 - var connectionType = flipflopContext.State.ToContentType(); - if (connectionType != ConnectionType.None) + + singleFlipFlopNode.FlowData = await singleFlipFlopNode.ExecutingAsync(context); + if (singleFlipFlopNode.NextOrientation != ConnectionType.None) { - await GlobalFlipflopExecute(context, singleFlipFlopNode, connectionType); + var nextNodes = singleFlipFlopNode.SuccessorNodes[singleFlipFlopNode.NextOrientation]; + for (int i = nextNodes.Count - 1; i >= 0; i--) + { + if (nextNodes[i].DebugSetting.IsEnable) // 排除未启用的后继节点 + { + nextNodes[i].PreviousNode = singleFlipFlopNode; + await nextNodes[i].StartExecute(context); // 执行流程节点的后继分支 + } + } } + //if(t) + //{ + // IFlipflopContext flipflopContext = await ((Func>)del.Clone()).Invoke(md.ActingInstance);// 开始等待全局触发器的触发 + // var connectionType = flipflopContext.State.ToContentType(); + // if (connectionType != ConnectionType.None) + // { + // await GlobalFlipflopExecute(context, singleFlipFlopNode, connectionType, cts); + // } + //} + //else + //{ + // IFlipflopContext flipflopContext = await ((Func>)del.Clone()).Invoke(md.ActingInstance, parameters);// 开始等待全局触发器的触发 + // var connectionType = flipflopContext.State.ToContentType(); + // if (connectionType != ConnectionType.None) + // { + // await GlobalFlipflopExecute(context, singleFlipFlopNode, connectionType, cts); + // } + //} + } } catch (Exception ex) { await Console.Out.WriteLineAsync(ex.ToString()); } + finally + { + cts?.Cancel(); + } } /// @@ -292,15 +363,12 @@ namespace Serein.NodeFlow /// 被触发的全局触发器 /// 分支类型 /// - public async Task GlobalFlipflopExecute(IDynamicContext context, SingleFlipflopNode singleFlipFlopNode, ConnectionType connectionType) + public async Task GlobalFlipflopExecute(IDynamicContext context, SingleFlipflopNode singleFlipFlopNode, + ConnectionType connectionType, CancellationTokenSource cts) { - if (FlipFlopCts.IsCancellationRequested ) - { - return; - } + bool skip = true; - var cts = context.SereinIoc.GetOrRegisterInstantiate(); Stack stack = new Stack(); stack.Push(singleFlipFlopNode); @@ -311,10 +379,10 @@ namespace Serein.NodeFlow var currentNode = stack.Pop(); // 设置方法执行的对象 - if (currentNode.MethodDetails?.ActingInstance == null && currentNode.MethodDetails?.ActingInstanceType is not null) - { - currentNode.MethodDetails.ActingInstance ??= context.SereinIoc.GetOrRegisterInstantiate(currentNode.MethodDetails.ActingInstanceType); - } + //if (currentNode.MethodDetails?.ActingInstance == null && currentNode.MethodDetails?.ActingInstanceType is not null) + //{ + // currentNode.MethodDetails.ActingInstance ??= context.SereinIoc.GetOrRegisterInstantiate(currentNode.MethodDetails.ActingInstanceType); + //} // 首先执行上游分支 var upstreamNodes = currentNode.SuccessorNodes[ConnectionType.Upstream]; @@ -331,7 +399,8 @@ namespace Serein.NodeFlow } else { - currentNode.FlowData = await currentNode.ExecutingAsync(context); + currentNode.FlowData = await currentNode.ExecutingAsync(context); + if (currentNode.NextOrientation == ConnectionType.None) { diff --git a/NodeFlow/Model/SingleActionNode.cs b/NodeFlow/Model/SingleActionNode.cs index b4b5323..1bdb005 100644 --- a/NodeFlow/Model/SingleActionNode.cs +++ b/NodeFlow/Model/SingleActionNode.cs @@ -15,12 +15,12 @@ namespace Serein.NodeFlow.Model if (base.MethodDetails.ExplicitDatas.Length > 0) { return MethodDetails.ExplicitDatas - .Select(it => new Parameterdata - { - State = it.IsExplicitData, - Value = it.DataValue, - }) - .ToArray(); + .Select(it => new Parameterdata + { + State = it.IsExplicitData, + Value = it.DataValue, + }) + .ToArray(); } else { diff --git a/NodeFlow/Model/SingleFlipflopNode.cs b/NodeFlow/Model/SingleFlipflopNode.cs index edd0b8f..7ec4903 100644 --- a/NodeFlow/Model/SingleFlipflopNode.cs +++ b/NodeFlow/Model/SingleFlipflopNode.cs @@ -22,31 +22,49 @@ namespace Serein.NodeFlow.Model public override async Task ExecutingAsync(IDynamicContext context) { #region 执行前中断 - if (TryCreateInterruptTask(context, this, out Task? task)) + if (DebugSetting.IsInterrupt && TryCreateInterruptTask(context, this, out Task? task)) // 执行触发前 { + string guid = this.Guid.ToString(); + this.CancelInterruptCallback ??= () => context.FlowEnvironment.ChannelFlowInterrupt.TriggerSignal(guid); var cancelType = await task!; + task?.ToString(); await Console.Out.WriteLineAsync($"[{this.MethodDetails.MethodName}]中断已{(cancelType == CancelType.Manual ? "手动取消" : "自动取消")},开始执行后继分支"); } #endregion MethodDetails md = MethodDetails; - Delegate del = md.MethodDelegate; + var del = md.MethodDelegate.Clone(); object instance = md.ActingInstance; - var haveParameter = md.ExplicitDatas.Length >= 0; + // Task? flipflopTask = null; try { // 调用委托并获取结果 - Task flipflopTask = haveParameter switch + Task flipflopTask = md.ExplicitDatas.Length switch { - true => ((Func>)del).Invoke(instance, GetParameters(context, md)), // 执行流程中的触发器方法时获取入参参数 - false => ((Func>)del).Invoke(instance), + 0 => ((Func>)del).Invoke(md.ActingInstance), + _ => ((Func>)del).Invoke(md.ActingInstance, GetParameters(context, md)), // 执行流程中的触发器方法时获取入参参数 }; - + //object?[]? parameters; + //object? result = null; + //if (haveParameter) + //{ + // var data = GetParameters(context, md); + // parameters = [instance, data]; + //} + //else + //{ + // parameters = [instance]; + //} + //flipflopTask = del.DynamicInvoke(parameters) as Task; + //if (flipflopTask == null) + //{ + // throw new FlipflopException(base.MethodDetails.MethodName + "触发器返回值非 Task 类型"); + //} IFlipflopContext flipflopContext = (await flipflopTask) ?? throw new FlipflopException("没有返回上下文"); NextOrientation = flipflopContext.State.ToContentType(); - if(flipflopContext.TriggerData.Type == Library.NodeFlow.Tool.TriggerType.Overtime) + if(flipflopContext.TriggerData is null || flipflopContext.TriggerData.Type == Library.NodeFlow.Tool.TriggerType.Overtime) { - throw new FlipflopException(""); + throw new FlipflopException(base.MethodDetails.MethodName + "触发器超时触发。Guid"+base.Guid); } return flipflopContext.TriggerData.Value; } @@ -62,6 +80,10 @@ namespace Serein.NodeFlow.Model RuningException = ex; return null; } + finally + { + // flipflopTask?.Dispose(); + } } internal override Parameterdata[] GetParameterdatas() diff --git a/WorkBench/LogWindow.xaml.cs b/WorkBench/LogWindow.xaml.cs index 85e7e16..7e12f7a 100644 --- a/WorkBench/LogWindow.xaml.cs +++ b/WorkBench/LogWindow.xaml.cs @@ -5,21 +5,102 @@ namespace Serein.WorkBench /// /// DebugWindow.xaml 的交互逻辑 /// + using System; + using System.Text; + using System.Threading.Tasks; + using System.Timers; + using System.Windows; + public partial class LogWindow : Window { + private StringBuilder logBuffer = new StringBuilder(); + private int logUpdateInterval = 100; // 批量更新的时间间隔(毫秒) + private Timer logUpdateTimer; + private const int MaxLines = 1000; // 最大显示的行数 + private bool autoScroll = true; // 自动滚动标识 + public LogWindow() { InitializeComponent(); + + // 初始化定时器,用于批量更新日志 + logUpdateTimer = new Timer(logUpdateInterval); + logUpdateTimer.Elapsed += (s, e) => FlushLog(); // 定时刷新日志 + logUpdateTimer.Start(); + + // 添加滚动事件处理,判断用户是否手动滚动 + // LogTextBox.ScrollChanged += LogTextBox_ScrollChanged; } + /// + /// 添加日志到缓冲区 + /// public void AppendText(string text) { - Dispatcher.BeginInvoke(() => + lock (logBuffer) { - LogTextBox.AppendText(text); - LogTextBox.ScrollToEnd(); + logBuffer.Append(text); // 将日志添加到缓冲区中 + } + } + + /// + /// 清空日志缓冲区并更新到 TextBox 中 + /// + private void FlushLog() + { + if (logBuffer.Length == 0) return; + + Dispatcher.Invoke(() => + { + lock (logBuffer) + { + LogTextBox.AppendText(logBuffer.ToString()); + logBuffer.Clear(); // 清空缓冲区 + } + + TrimLog(); // 检查并修剪日志长度 + ScrollToEndIfNeeded(); // 根据条件滚动到末尾 }); } + + /// + /// 限制日志输出的最大行数,超出时删除旧日志 + /// + private void TrimLog() + { + if (LogTextBox.LineCount > MaxLines) + { + // 删除最早的多余行 + LogTextBox.Text = LogTextBox.Text.Substring(LogTextBox.GetCharacterIndexFromLineIndex(LogTextBox.LineCount - MaxLines)); + } + } + + /// + /// 检测用户是否手动滚动了文本框 + /// + private void LogTextBox_ScrollChanged(object sender, System.Windows.Controls.ScrollChangedEventArgs e) + { + if (e.ExtentHeightChange == 0) // 用户手动滚动时 + { + // 判断是否滚动到底部 + // autoScroll = LogTextBox.VerticalOffset == LogTextBox.ScrollableHeight; + } + } + + /// + /// 根据 autoScroll 标志决定是否滚动到末尾 + /// + private void ScrollToEndIfNeeded() + { + if (autoScroll) + { + LogTextBox.ScrollToEnd(); // 仅在需要时滚动到末尾 + } + } + + /// + /// 清空日志 + /// public void Clear() { Dispatcher.BeginInvoke(() => @@ -27,15 +108,28 @@ namespace Serein.WorkBench LogTextBox.Clear(); }); } + + /// + /// 点击清空日志按钮时触发 + /// private void ClearLog_Click(object sender, RoutedEventArgs e) { LogTextBox.Clear(); } + /// + /// 窗口关闭事件,隐藏窗体而不是关闭 + /// private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { + //logUpdateTimer?.Stop(); + //logUpdateTimer?.Close(); + //logUpdateTimer?.Dispose(); + logBuffer?.Clear(); + Clear(); e.Cancel = true; // 取消关闭操作 this.Hide(); // 隐藏窗体而不是关闭 } } + } diff --git a/WorkBench/MainWindow.xaml.cs b/WorkBench/MainWindow.xaml.cs index 01d6187..4f22e42 100644 --- a/WorkBench/MainWindow.xaml.cs +++ b/WorkBench/MainWindow.xaml.cs @@ -87,6 +87,7 @@ namespace Serein.WorkBench /// 标记是否正在拖动画布 /// private bool IsCanvasDragging; + private bool IsSelectDragging; /// /// 当前选取的控件 @@ -823,8 +824,9 @@ namespace Serein.WorkBench } } - if (IsSelectControl /*&& e.LeftButton == MouseButtonState.Pressed*/) // 正在选取节点 + if (IsSelectControl) // 正在选取节点 { + IsSelectDragging = e.LeftButton == MouseButtonState.Pressed; // 获取当前鼠标位置 Point currentPoint = e.GetPosition(FlowChartCanvas); @@ -1407,6 +1409,36 @@ namespace Serein.WorkBench /// private void FlowChartCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { + if (!IsSelectControl) + { + // 进入选取状态 + IsSelectControl = true; + IsSelectDragging = false; // 初始化为非拖动状态 + + // 记录鼠标起始点 + startSelectControolPoint = e.GetPosition(FlowChartCanvas); + + // 初始化选取矩形的位置和大小 + Canvas.SetLeft(SelectionRectangle, startSelectControolPoint.X); + Canvas.SetTop(SelectionRectangle, startSelectControolPoint.Y); + SelectionRectangle.Width = 0; + SelectionRectangle.Height = 0; + + // 显示选取矩形 + SelectionRectangle.Visibility = Visibility.Visible; + SelectionRectangle.ContextMenu ??= ConfiguerSelectionRectangle(); + + // 捕获鼠标,以便在鼠标移动到Canvas外部时仍能处理事件 + FlowChartCanvas.CaptureMouse(); + } + else + { + // 如果已经是选取状态,单击则认为结束框选 + CompleteSelection(); + } + + e.Handled = true; // 防止事件传播影响其他控件 + return; // 如果正在选取状态,再次点击画布时自动确定选取范围,否则进入选取状态 if (IsSelectControl) { @@ -1467,14 +1499,71 @@ namespace Serein.WorkBench } + /// + /// 在画布中释放鼠标按下,结束选取状态 + /// + /// + /// + private void FlowChartCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) + { + if (IsSelectControl) + { + // 松开鼠标时判断是否为拖动操作 + if (IsSelectDragging) + { + // 完成拖动框选 + CompleteSelection(); + } + + // 释放鼠标捕获 + FlowChartCanvas.ReleaseMouseCapture(); + } + + e.Handled = true; + } + + /// 完成选取操作 + /// + private void CompleteSelection() + { + IsSelectControl = false; + + // 隐藏选取矩形 + SelectionRectangle.Visibility = Visibility.Collapsed; + + // 获取选取范围 + Rect selectionArea = new Rect(Canvas.GetLeft(SelectionRectangle), + Canvas.GetTop(SelectionRectangle), + SelectionRectangle.Width, + SelectionRectangle.Height); + + // 处理选取范围内的控件 + // selectNodeControls.Clear(); + foreach (UIElement element in FlowChartCanvas.Children) + { + Rect elementBounds = new Rect(Canvas.GetLeft(element), Canvas.GetTop(element), + element.RenderSize.Width, element.RenderSize.Height); + + if (selectionArea.Contains(elementBounds)) + { + if (element is NodeControlBase control) + { + selectNodeControls.Add(control); + } + } + } + + // 选中后的操作 + SelectedNode(); + } private ContextMenu ConfiguerSelectionRectangle() { var contextMenu = new ContextMenu(); contextMenu.Items.Add(CreateMenuItem("删除", (s, e) => { - if(selectNodeControls.Count > 0) + if (selectNodeControls.Count > 0) { - foreach(var node in selectNodeControls.ToArray()) + foreach (var node in selectNodeControls.ToArray()) { var guid = node?.ViewModel?.Node?.Guid; if (!string.IsNullOrEmpty(guid)) @@ -1488,25 +1577,11 @@ namespace Serein.WorkBench return contextMenu; // nodeControl.ContextMenu = contextMenu; } - - /// - /// 在画布中释放鼠标按下,结束选取状态 - /// - /// - /// - private void FlowChartCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) - { - if (IsSelectControl) - { - - } - } - private void SelectedNode() { if(selectNodeControls.Count == 0) { - Console.WriteLine($"没有选择控件"); + //Console.WriteLine($"没有选择控件"); SelectionRectangle.Visibility = Visibility.Collapsed; return; } @@ -1514,7 +1589,7 @@ namespace Serein.WorkBench foreach (var node in selectNodeControls) { node.ViewModel.Selected(); - node.ViewModel.CancelSelect(); + // node.ViewModel.CancelSelect(); node.BorderBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFC700")); node.BorderThickness = new Thickness(4); } @@ -1849,8 +1924,8 @@ namespace Serein.WorkBench await FlowEnvironment.StartAsync(); // 快 - //await Task.Run( FlowEnvironment.StartAsync); // 上下文多次切换的场景中吗慢了1/5 - //await Task.Factory.StartNew(FlowEnvironment.StartAsync); // 慢了1/5 + //await Task.Run( FlowEnvironment.StartAsync); // 上下文多次切换的场景中慢了1/10,定时器精度丢失 + //await Task.Factory.StartNew(FlowEnvironment.StartAsync); // 慢了1/5,定时器精度丢失 } /// @@ -1975,6 +2050,11 @@ namespace Serein.WorkBench /// private void Window_PreviewKeyDown(object sender, KeyEventArgs e) { + if (e.Key == Key.Tab) + { + e.Handled = true; // 禁止默认的Tab键行为 + } + if (e.KeyStates == Keyboard.GetKeyStates(Key.Escape)) //if (Keyboard.Modifiers == ModifierKeys.Shift) { @@ -1984,6 +2064,7 @@ namespace Serein.WorkBench EndConnection(); SelectionRectangle.Visibility = Visibility.Collapsed; CancelSelectNode(); + } } diff --git a/WorkBench/Node/NodeControlViewModelBase.cs b/WorkBench/Node/NodeControlViewModelBase.cs index 0e8d1e5..eea3145 100644 --- a/WorkBench/Node/NodeControlViewModelBase.cs +++ b/WorkBench/Node/NodeControlViewModelBase.cs @@ -94,7 +94,7 @@ namespace Serein.WorkBench.Node.ViewModel // } //} - public event PropertyChangedEventHandler PropertyChanged; + public event PropertyChangedEventHandler? PropertyChanged; protected void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); diff --git a/WorkBench/tool/LogTextWriter.cs b/WorkBench/tool/LogTextWriter.cs index 13d0575..4b3c339 100644 --- a/WorkBench/tool/LogTextWriter.cs +++ b/WorkBench/tool/LogTextWriter.cs @@ -1,6 +1,7 @@ using System.Collections.Concurrent; using System.IO; using System.Text; +using System.Threading.Channels; namespace Serein.WorkBench.tool { @@ -9,21 +10,20 @@ namespace Serein.WorkBench.tool /// public class LogTextWriter : TextWriter { - private readonly Action logAction; - private readonly StringWriter stringWriter = new(); - private readonly BlockingCollection logQueue = new(); - private readonly Task logTask; - - // 用于计数的字段 - private int writeCount = 0; - private const int maxWrites = 500; - private readonly Action clearTextBoxAction; + private readonly Action logAction; // 更新日志UI的委托 + private readonly StringWriter stringWriter = new(); // 缓存日志内容 + private readonly Channel logChannel = Channel.CreateUnbounded(); // 日志管道 + private readonly Action clearTextBoxAction; // 清空日志UI的委托 + private int writeCount = 0; // 写入计数器 + private const int maxWrites = 500; // 写入最大计数 public LogTextWriter(Action logAction, Action clearTextBoxAction) { this.logAction = logAction; this.clearTextBoxAction = clearTextBoxAction; - logTask = Task.Run(ProcessLogQueue); // 异步处理日志 + + // 异步启动日志处理任务,不阻塞主线程 + Task.Run(ProcessLogQueueAsync); } public override Encoding Encoding => Encoding.UTF8; @@ -54,39 +54,32 @@ namespace Serein.WorkBench.tool EnqueueLog(); } + // 将日志加入通道 private void EnqueueLog() { - logQueue.Add(stringWriter.ToString()); + var log = stringWriter.ToString(); stringWriter.GetStringBuilder().Clear(); - } - private async Task ProcessLogQueue() - { - foreach (var log in logQueue.GetConsumingEnumerable()) + if (!logChannel.Writer.TryWrite(log)) { - // 异步执行日志输出操作 - await Task.Run(() => - { - logAction(log); - - // 计数器增加 - writeCount++; - if (writeCount >= maxWrites) - { - // 计数器达到50,清空文本框 - clearTextBoxAction?.Invoke(); - writeCount = 0; // 重置计数器 - } - }); + // 如果写入失败(不太可能),则直接丢弃日志或处理 } } - public new void Dispose() + // 异步处理日志队列 + private async Task ProcessLogQueueAsync() { - logQueue.CompleteAdding(); - logTask.Wait(); - base.Dispose(); + await foreach (var log in logChannel.Reader.ReadAllAsync()) // 异步读取日志通道 + { + logAction?.Invoke(log); // 执行日志写入到UI的委托 + + writeCount++; + if (writeCount >= maxWrites) + { + clearTextBoxAction?.Invoke(); // 清空文本框 + writeCount = 0; // 重置计数器 + } + } } } - }