123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Diagnostics;
- #pragma warning disable 0414
- namespace MECF.Framework.Common.Routine
- {
- public class RoutineRunner
- {
- public enum State
- {
- Init,
- Running,
- End,
- Failed,
- Timeout,
- }
-
- private State _subState = State.Init;
- private Stack<int> _step = new Stack<int>();
- private int _curStep = int.MaxValue;
- private State _runnerState = State.Init;
- Stopwatch _subStepTimer = new Stopwatch();
- public RoutineRunner()
- {
- _runnerState = State.Init;
- }
- public void Reset()
- {
- _runnerState = State.Init;
- _subState = State.Init;
- _step.Clear();
- }
- public RoutineRunner Start(int id, Func<bool> action, Func<bool> condition, int timeout)
- {
- if (!_step.Contains(id))
- {
- _runnerState = State.Running;
- startNewStep(id, action);
- }
- else
- {
- if (_subState == State.Running)
- {
- if(condition())
- {
- _subState = State.End;
- }
- else
- {
- if (_subStepTimer.ElapsedMilliseconds > timeout)
- {
- _runnerState = State.Failed;
- _subState = State.Timeout;
- }
- }
- }
- }
-
- return this;
- }
- public RoutineRunner Start(int id, Func<bool> action, int timeout)
- {
- if (!_step.Contains(id))
- {
- _runnerState = State.Running;
- startNewStep(id, action);
- }
- else
- {
- if (_subStepTimer.ElapsedMilliseconds > timeout)
- {
- _subState = State.End;
- }
- }
- return this;
- }
- public RoutineRunner Continue(int id, Func<bool> action, Func<bool> condition, int timeout)
- {
- if (_subState == State.End && !_step.Contains(id))
- {
- startNewStep(id, action);
- }
- else if(_curStep == id && _subState == State.Running)
- {
- if (condition())
- {
- _subState = State.End;
- }
- else
- {
- if (_subStepTimer.ElapsedMilliseconds > timeout)
- {
- _runnerState = State.Failed;
- _subState = State.Timeout;
- }
- }
- }
- return this;
- }
- public RoutineRunner Continue(int id, Func<bool> action, int timeout)
- {
- if (_subState == State.End && !_step.Contains(id))
- {
- startNewStep(id, action);
- }
- else if (_curStep == id && _subState == State.Running)
- {
- if (_subStepTimer.ElapsedMilliseconds > timeout)
- {
- _subState = State.End;
- }
- }
- return this;
- }
- public RoutineRunner End(int id, Func<bool> action, Func<bool> condition, int timeout)
- {
- if (_subState == State.End && !_step.Contains(id))
- {
- startNewStep(id, action);
- }
- else if (_curStep == id && _subState == State.Running)
- {
- if (condition())
- {
- _runnerState = State.End;
- _subState = State.End;
- }
- else
- {
- if (_subStepTimer.ElapsedMilliseconds > timeout)
- {
- _runnerState = State.Failed;
- _subState = State.Timeout;
- }
- }
- }
- return this;
- }
- public RoutineRunner End(int id, Func<bool> action, int timeout)
- {
- if (_subState == State.End && !_step.Contains(id))
- {
- startNewStep(id, action);
- }
- else if (_curStep == id && _subState == State.Running)
- {
- if (_subStepTimer.ElapsedMilliseconds > timeout)
- {
- _runnerState = State.End;
- _subState = State.End;
- }
- }
- return this;
- }
- private void startNewStep(int id, Func<bool> action)
- {
- if (action())
- {
- _step.Push(id);
- _curStep = id;
- _subState = State.Running;
- _subStepTimer.Restart();
- }
- else
- {
- _runnerState = State.Failed;
- }
- }
- }
- }
|