Skip to content

Commit

Permalink
优化 情景遇到错误自动停止运行(Tick)
Browse files Browse the repository at this point in the history
  • Loading branch information
MakesYT committed May 24, 2024
1 parent df8504e commit 5b6af23
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions Core/SDKs/CustomScenario/CustomScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -333,6 +338,7 @@ private void TickMethod(object sender, long JumpPeriod, long interval)
if (_cancellationTokenSource.Token.IsCancellationRequested)
{
InTick = false;
_tickUtil.Dispose();
break;
}

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -784,6 +800,8 @@ private void ParsePointItem(Dictionary<PointItem, Thread?> 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;
}
Expand Down

0 comments on commit 5b6af23

Please sign in to comment.