AsyncWorkState.cs 1.8 KB

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