public Boolean CallFSM(SafeID tID, String tReturnID, params Object[] tArgs)
        {
            if (!this.Available)
            {
                return(false);
            }
            else if (!this.IsActive)
            {
                return(false);
            }

            lock (m_Locker)
            {
                State tTarget = m_FreeStateSet.Find(tID);
                if (null != tTarget)
                {
                    if (!tTarget.Available)
                    {
                        return(false);
                    }


                    if (tTarget.IsFSM)
                    {
                        IFSM tFSM = tTarget as IFSM;
                        if (null == tFSM)
                        {
                            return(false);
                        }
                        m_FreeStateSet.Remove(tID);
                        m_ActiveFSMSet.Add(tTarget);
                        tFSM.Reset();
                        do
                        {
                            miniFSM tminiFSM = tFSM as miniFSM;
                            if (null == tminiFSM)
                            {
                                break;
                            }
                            tminiFSM.m_ReturnStateID = tReturnID;
                        }while (false);

                        tFSM.Start(tArgs);
                    }
                    else
                    {
                        m_FreeStateSet.Remove(tID);
                        m_ActiveStateSet.Add(tTarget);
                    }
                }
            }

            m_StateChanged = true;

            RefreshState();

            return(true);
        }
        public override Status Task(params object[] tArgs)
        {
            if (!this.Available)
            {
                return(m_Status);
            }


            do
            {
                switch (m_Status)
                {
                case Status.FSM_STARTING:
                    m_Status = Status.FSM_WORKING;
                    break;

                case Status.FSM_CLOSING:
                case Status.FSM_PENDING:
                case Status.FSM_WORKING:
                    break;

                case Status.FSM_ERROR:
                case Status.FSM_INVALID:
                case Status.FSM_CANCELLED:
                case Status.FSM_CLOSED:
                case Status.FSM_IDLE:
                default:
                    return(m_Status);
                }

                //! stop event
                if (m_Event.WaitOne(0))
                {
                    lock (m_Locker)
                    {
                        foreach (State tItem in m_ActiveStateSet)
                        {
                            if (null == tItem)
                            {
                                continue;
                            }
                            else if (!tItem.Available)
                            {
                                continue;
                            }
                            if ((tItem.IsActive) || (tItem.Status == Status.FSM_IDLE))
                            {
                                tItem.StopSignal.Set();
                            }
                        }

                        foreach (State tItem in m_ActiveFSMSet)
                        {
                            if (null == tItem)
                            {
                                continue;
                            }
                            else if (!tItem.Available)
                            {
                                continue;
                            }
                            IFSM tFSM = tItem as IFSM;
                            if (null == tFSM)
                            {
                                continue;
                            }
                            if ((tFSM.IsActive) || (tFSM.Status == Status.FSM_IDLE))
                            {
                                tFSM.RequestStop();
                            }
                        }
                        m_Event.Reset();
                    }
                }

                m_StateChanged = false;                     //!< reset state change flag


                lock (m_Locker)
                {
                    State[] tActiveStates = m_ActiveStateSet.ToArray();
                    foreach (State tItem in tActiveStates)
                    {
                        if (null == tItem)
                        {
                            continue;
                        }
                        else if (!tItem.Available)
                        {
                            continue;
                        }

                        switch (tItem.Task(m_Args))
                        {
                        case Status.FSM_PENDING:
                        case Status.FSM_CLOSING:
                        case Status.FSM_STARTING:
                        case Status.FSM_WORKING:

                            break;

                        case Status.FSM_ERROR:
                            //! encountered an unhandled error
                            if (ErrorHandler(m_Args))
                            {
                                continue;
                            }
                            m_Status = Status.FSM_ERROR;
                            return(m_Status);

                        case Status.FSM_INVALID:
                        case Status.FSM_CANCELLED:
                        case Status.FSM_CLOSED:
                        case Status.FSM_IDLE:
                        default:
                            m_FreeStateSet.Add(tItem);
                            m_ActiveStateSet.Remove(tItem.ID);
                            m_StateChanged = true;
                            break;
                        }
                    }

                    tActiveStates = m_ActiveFSMSet.ToArray();
                    foreach (State tItem in tActiveStates)
                    {
                        if (null == tItem)
                        {
                            continue;
                        }
                        else if (!tItem.Available)
                        {
                            continue;
                        }
                        switch (tItem.Task(m_Args))
                        {
                        case Status.FSM_ERROR:
                            m_Status = Status.FSM_ERROR;
                            return(m_Status);

                        case Status.FSM_CLOSING:
                        case Status.FSM_PENDING:
                        case Status.FSM_STARTING:
                        case Status.FSM_WORKING:
                            break;

                        case Status.FSM_INVALID:
                        case Status.FSM_CANCELLED:
                        case Status.FSM_CLOSED:
                        case Status.FSM_IDLE:
                        default:
                            m_FreeStateSet.Add(tItem);
                            m_ActiveFSMSet.Remove(tItem.ID);

                            m_StateChanged = true;

                            IFSM tFSM = tItem as IFSM;
                            if (null != tFSM)
                            {
                                tFSM.Reset();
                            }

                            do
                            {
                                miniFSM tminiFSM = tItem as miniFSM;
                                if (null == tminiFSM)
                                {
                                    break;
                                }
                                else if (null == tminiFSM.ReturnStateID)
                                {
                                    break;
                                }
                                this.TriggerFSM(tminiFSM.ReturnStateID, m_Args);
                            }while (false);
                            break;
                        }
                    }
                }

                RefreshState();
            }while (m_StateChanged);

            return(m_Status);
        }
 public miniFSM(String tID, AutoResetEvent tEvent, miniFSM tParent)
     : base(tID, null, tEvent, tParent)
 {
     Initialize();
 }
 public miniFSM(String tID, AutoResetEvent tEvent, miniFSM tParent, params Object[] tArgs)
     : base(tID, null, tEvent, tParent)
 {
     m_Args = tArgs;
     Initialize();
 }
 public miniFSM(String tID, miniFSM tParent)
     : base(tID, null, null, tParent)
 {
     Initialize();
 }