123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- #region --------------- Copyright Dresser Wayne Pignone -------------
- /*
- * $Log: /Wrk/WayneLibraries/Wrk/StateEngine/Generic/AsyncWorkState.cs $
- *
- * 5 08-03-25 10:32 Mattias.larsson
- * Ensure debugLogger is not null.
- *
- * 4 07-11-28 15:40 roger.månsson
- *
- * 3 07-10-23 13:47 roger.månsson
- * Must protect the access to the field currentWork, or there may be race
- * conditions, the field is maybe not be assigned until after the work is
- * done for example.
- *
- * 2 07-10-17 13:20 roger.månsson
- * Made publicly available. Added overridable method WorkDone.
- *
- * 1 07-03-27 9:33 roger.månsson
- * Created.
- */
- #endregion
- using System;
- using System.Threading;
- namespace Wayne.Lib.StateEngine.Generic
- {
- /// <summary>
- /// Generic state that enables descendant classes to execute code on a worker thread
- /// when the state is active. When the processing is completed, i.e. the PerformWork returns,
- /// the state will post a transition of the type specified in the constructor.
- ///
- /// When exitting the state, we for the worker thread to complete before continuing.
- ///
- /// Descendant classes can also use the AbortWork() method to signal to the worker thread that it should exit as fast as possible.
- ///
- /// The PerformWork method should periodically check the Aborted property and if that is true, exit as fast as possible.
- /// </summary>
- /// <typeparam name="TMain"></typeparam>
- public abstract class AsyncWorkState<TMain> : State<TMain>
- {
- #region Fields
- bool aborted;
- object doneTransitionType;
- object runningLock = new object();
- #endregion
- #region Construction
- /// <summary>
- /// Initializes a new instance of the AsyncWorkState class.
- /// </summary>
- protected AsyncWorkState(object doneTransitionType)
- {
- this.doneTransitionType = doneTransitionType;
- }
- #endregion
- #region Methods
- /// <summary>
- /// Called when entering the state.
- /// </summary>
- /// <param name="stateEntry"></param>
- /// <param name="transition"></param>
- 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));
- }
- }
- /// <summary>
- /// Called when exitting the state.
- /// </summary>
- protected override void Exit()
- {
- base.Exit();
- aborted = true;
- lock (runningLock)
- {
- }
-
- }
- /// <summary>
- /// Handles events.
- /// </summary>
- /// <param name="stateEngineEvent"></param>
- /// <param name="transition"></param>
- 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);
- }
- }
- /// <summary>
- /// Method that is called when the work is done. Override to specify another transition type than the one
- /// specified in the constructor.
- /// </summary>
- /// <param name="transition"></param>
- protected virtual void WorkDone(ref Transition transition)
- {
- transition = new Transition(this, doneTransitionType);
- }
- /// <summary>
- /// Signals to the thread that it should exit as quick as possible. Can be overridden by descendant
- /// classes to create user code to abort.
- /// </summary>
- protected virtual void AbortWork()
- {
- aborted = true;
- }
- /// <summary>
- /// Signals that the PerformWork method should return as fast as possible.
- /// </summary>
- protected bool Aborted { get { return aborted; } }
- #endregion
- #region Abstract methods
- /// <summary>
- /// Method that is called in a worker thread.
- /// </summary>
- protected abstract void PerformWork();
- #endregion
- }
- }
|