123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- #region --------------- Copyright Dresser Wayne Pignone -------------
- #endregion
- using System;
- using System.Collections.Generic;
- using Wayne.Lib;
- using Wayne.Lib.StateEngine;
- using System.Threading;
- namespace Wayne.Lib.StateEngine.Generic
- {
-
-
-
-
-
-
-
-
-
-
-
-
- public abstract class AsyncWorkState<TMain> : State<TMain>
- {
- #region Fields
- bool aborted;
- object doneTransitionType;
- object runningLock = new object();
- #endregion
- #region Construction
-
-
-
- protected AsyncWorkState(object doneTransitionType)
- {
- this.doneTransitionType = doneTransitionType;
- }
- #endregion
- #region Methods
-
-
-
-
-
- protected override void Enter(Wayne.Lib.StateEngine.StateEntry stateEntry, ref Wayne.Lib.StateEngine.Transition transition)
- {
- base.Enter(stateEntry, ref transition);
- if (transition == null)
- {
- aborted = false;
- ThreadPool.QueueUserWorkItem(new WaitCallback(DoWork), null);
- }
- }
- private void DoWork(object o)
- {
- try
- {
- try
- {
- if (aborted)
- return;
- lock (runningLock)
- {
- PerformWork();
- }
- }
- catch (Exception e)
- {
- if ((ParentStateMachine.debugLogger != null) && ParentStateMachine.debugLogger.IsActive())
- ParentStateMachine.debugLogger.Add(e);
- }
- }
- finally
- {
- this.ParentStateMachine.IncomingEvent(new StateEngineEvent(GenericEventType.AsyncDone));
- }
- }
-
-
-
- protected override void Exit()
- {
- base.Exit();
- aborted = true;
- lock (runningLock)
- {
- }
-
- }
-
-
-
-
-
- protected override void HandleEvent(StateEngineEvent stateEngineEvent, ref Transition transition)
- {
- base.HandleEvent(stateEngineEvent, ref transition);
- if (stateEngineEvent.Type.Equals(GenericEventType.AsyncDone))
- {
- stateEngineEvent.Handled = true;
- WorkDone(ref transition);
- }
- }
-
-
-
-
-
- protected virtual void WorkDone(ref Transition transition)
- {
- transition = new Transition(this, doneTransitionType);
- }
-
-
-
-
- protected virtual void AbortWork()
- {
- aborted = true;
- }
-
-
-
- protected bool Aborted { get { return aborted; } }
- #endregion
- #region Abstract methods
-
-
-
- protected abstract void PerformWork();
- #endregion
- }
- }
|