Skip to content

Commit 3a81b81

Browse files
YuriYuri
authored andcommitted
Merge pull request TorchAPI#200 from N1Ran/master
loop fix
1 parent d0714f9 commit 3a81b81

2 files changed

Lines changed: 10 additions & 11 deletions

File tree

‎Essentials/AutoCommand.cs‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class AutoCommand : ViewModel
2626
private string _name;
2727
private float _triggerRatio;
2828
private double _triggerCount;
29+
private bool _isRunning;
2930

3031
[XmlIgnore]
3132
public bool Completed { get; set; }
@@ -187,24 +188,22 @@ internal async void RunNow()
187188
{
188189
_cTokenSource = new CancellationTokenSource();
189190
var token = _cTokenSource.Token;
190-
191+
_isRunning = true;
191192
await Task.Run(() =>
192193
{
193-
while (!token.IsCancellationRequested)
194+
foreach (var step in Steps)
194195
{
195-
foreach (var step in Steps)
196+
if (token.IsCancellationRequested)
196197
{
197-
if (token.IsCancellationRequested)
198-
{
199-
break;
200-
}
201-
step.RunStep();
202-
Thread.Sleep(step.DelaySpan);
198+
break;
203199
}
200+
step.RunStep();
201+
Thread.Sleep(step.DelaySpan);
204202
}
205203
}, token);
206204

207205
_cTokenSource.Dispose();
206+
_isRunning = false;
208207
}
209208

210209
internal void Cancel()
@@ -221,7 +220,7 @@ internal void Cancel()
221220

222221
internal bool IsRunning()
223222
{
224-
return _currentStep > 0 || _cTokenSource != null && _cTokenSource?.IsCancellationRequested == false;
223+
return _currentStep > 0 || _isRunning;
225224
}
226225
}
227226

‎Essentials/AutoCommands.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ private bool CanRun(AutoCommand command)
4040
case Trigger.Disabled:
4141
return false;
4242
case Trigger.OnStart:
43-
if (command.Completed) break;
43+
if (command.Completed || command.IsRunning())break;
4444
command.Completed = true;
4545
command.RunNow();
4646
break;

0 commit comments

Comments
 (0)