123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- using System;
- using System.Threading;
- using Wayne.Lib.Log;
- namespace Wayne.Lib.StateEngine
- {
- /// <summary>
- /// Threaded root state machine is the class that is driving a normal asynchronous state machine. It
- /// contains the logic for running a statemachine in a separate thread.
- /// </summary>
- //[System.Diagnostics.DebuggerNonUserCode()]
- internal class ThreadedRootStateMachine : EventBufferingRootStateMachine
- {
- #region Fields
- private System.Threading.Thread thread;
- private AutoResetEvent eventReceivedEvent = new AutoResetEvent(false);
- private AutoResetEvent threadStartedEvent = new AutoResetEvent(false);
- private bool threadTerminated;
- #endregion
- #region Construction
- /// <summary>
- /// Initializes a new instance of the ThreadedRootStateMachine class.
- /// </summary>
- /// <param name="name">The name of the statemachine.</param>
- /// <param name="debugLogger">The DebugLogger to use.</param>
- /// <param name="logCategory">The log category.</param>
- public ThreadedRootStateMachine(string name, IDebugLogger debugLogger, object logCategory)
- : base(name, debugLogger, logCategory)
- {
- thread = new System.Threading.Thread(new System.Threading.ThreadStart(Execute));
- }
- #endregion
- #region Private methods
- /// <summary>
- /// Thread method.
- /// </summary>
- private void Execute()
- {
- try
- {
- Initialize();
- PerformInitialTransition();
- //Signal to the thread calling Start() that it can continue to execute.
- threadStartedEvent.Set();
- CreateOnStateChangeEventHandler(this);
- while (!threadTerminated)
- {
- //Wait for new event to be added to the queue.
- eventReceivedEvent.WaitOne();
- if (!threadTerminated)
- {
- //Read out the event
- HandleQueuedEvents(null);
- }
- }
- }
- catch (Exception exception)
- {
- throw new StateEngineException(string.Format("Unexpected Exception in Execute(). StateMachine \"{0}\"", this.Name), exception);
- }
- }
- #endregion
- #region Public Methods
- /// <summary>
- /// Entry point for incoming events. The base functionality is removed, and moved into the thread.
- /// This method only puts the message into the event list, and signals to the thread that a new event has been added.
- /// </summary>
- /// <param name="stateEngineEvent"></param>
- public override void IncomingEvent(StateEngineEvent stateEngineEvent)
- {
- //Base EventBufferingRootStateMachine takes care of queueing the event.
- base.IncomingEvent(stateEngineEvent);
- //Signal to the thread that there is a new event to handle.
- if (eventReceivedEvent != null)
- eventReceivedEvent.Set();
- }
- /// <summary>
- /// Starts the state machine. The base functionality is removed and moved to the thread.
- /// </summary>
- public override void Start()
- {
- //Start the thread
- thread.Name = this.Name;
- thread.Start();
- //Wait until the thread is really started and initialized.
- threadStartedEvent.WaitOne();
- }
- #endregion
- #region Protected Methods
- /// <summary>
- /// Disposes the resources owned by the instance.
- /// </summary>
- /// <param name="disposing"></param>
- protected override void Dispose(bool disposing)
- {
- if (!disposed)
- {
- this.threadTerminated = true;
- eventReceivedEvent.Set();
- if (this.Started)
- {
- // JDL, MAR-02-10, wait up to 1 sec
- if (!thread.Join(1000))
- thread.Abort();
- thread = null;
- }
- if (eventReceivedEvent != null)
- {
- eventReceivedEvent.Close();
- eventReceivedEvent = null;
- }
- if (threadStartedEvent != null)
- {
- threadStartedEvent.Close();
- threadStartedEvent = null;
- }
- }
- base.Dispose(disposing);
- }
- #endregion
- }
- }
|