diff --git a/Core/SDKs/CustomScenario/CustomScenario.cs b/Core/SDKs/CustomScenario/CustomScenario.cs index 7b440d9..a8cd6e8 100644 --- a/Core/SDKs/CustomScenario/CustomScenario.cs +++ b/Core/SDKs/CustomScenario/CustomScenario.cs @@ -282,6 +282,11 @@ private void StartRun(bool notRealTime, bool onExit = false, params object[] inp return; } + if (_cancellationTokenSource.IsCancellationRequested) + { + return; + } + var connectionItem = connections.FirstOrDefault((e) => e.Source == nodes[1] .Output[0]); if (connectionItem == null || onExit) @@ -333,6 +338,7 @@ private void TickMethod(object sender, long JumpPeriod, long interval) if (_cancellationTokenSource.Token.IsCancellationRequested) { InTick = false; + _tickUtil.Dispose(); break; } @@ -366,16 +372,17 @@ private void TickMethod(object sender, long JumpPeriod, long interval) } } - public void Stop() + public void Stop(bool inTickError = false) { if (!IsRunning) { return; } - _tickUtil?.Dispose(); + try { + _tickUtil?.Dispose(); _cancellationTokenSource.Cancel(); } catch (Exception e) @@ -396,8 +403,17 @@ public void Stop() _initTasks.Clear(); _tickTasks.Clear(); IsRunning = false; - ((IToastService)ServiceManager.Services.GetService(typeof(IToastService))!).Show("情景", $"情景{Name}被用户停止"); - Log.Debug($"情景{Name}被用户停止"); + if (inTickError) + { + ((IToastService)ServiceManager.Services.GetService(typeof(IToastService))!) + .Show("情景", $"情景{Name}由于出现错误被停止"); + Log.Debug($"情景{Name}由于出现错误被停止"); + } + else + { + ((IToastService)ServiceManager.Services.GetService(typeof(IToastService))!).Show("情景", $"情景{Name}被用户停止"); + Log.Debug($"情景{Name}被用户停止"); + } } private void MakeSourcePointState(ConnectorItem targetConnectorItem, PointItem pointItem) @@ -784,6 +800,8 @@ private void ParsePointItem(Dictionary threads, PointItem no Log.Debug(e); ((IToastService)ServiceManager.Services.GetService(typeof(IToastService))!).Show("情景", $"情景{Name}出现错误\n{e.Message}"); + Task.Run(() => { Stop(true); }); + valid = false; goto finnish; }