AsyncWorkState.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Collections.Generic;
  3. using Wayne.Lib;
  4. using Wayne.Lib.StateEngine;
  5. using System.Threading;
  6. namespace Wayne.Lib.StateEngine.Generic
  7. {
  8. /// <summary>
  9. /// Generic state that enables descendant classes to execute code on a worker thread
  10. /// when the state is active. When the processing is completed, i.e. the PerformWork returns,
  11. /// the state will post a transition of the type specified in the constructor.
  12. ///
  13. /// When exitting the state, we for the worker thread to complete before continuing.
  14. ///
  15. /// Descendant classes can also use the AbortWork() method to signal to the worker thread that it should exit as fast as possible.
  16. ///
  17. /// The PerformWork method should periodically check the Aborted property and if that is true, exit as fast as possible.
  18. /// </summary>
  19. /// <typeparam name="TMain"></typeparam>
  20. /// <typeparam name="TData">Specifies the type of state data the state uses.</typeparam>
  21. public abstract class AsyncWorkState<TMain, TData> : AsyncWorkState<TMain> where TData : StateData
  22. {
  23. #region Fields
  24. private IStateWithData stateWithData;
  25. #endregion
  26. #region Construction
  27. /// <summary>
  28. /// Protected constructor.
  29. /// </summary>
  30. /// <param name="doneTransitionType"></param>
  31. protected AsyncWorkState(object doneTransitionType)
  32. : base(doneTransitionType)
  33. {
  34. }
  35. #endregion
  36. #region Properties
  37. /// <summary>
  38. /// Gets the state data for this state.
  39. /// </summary>
  40. protected TData Data
  41. {
  42. get
  43. {
  44. if (stateWithData == null)
  45. {
  46. stateWithData = StateData.GetParentCompositeStateWithStateData<TData>(this);
  47. }
  48. return stateWithData.StateData as TData;
  49. }
  50. }
  51. #endregion
  52. }
  53. }