/// <summary>
        /// Terminates the thread by causing the execution method to return.
        /// </summary>
        /// <param name="waitForTerminated">Flag indicating if the method should wait until the thread is terminated (true), or if it should return immediately (false).</param>
        /// <remarks>
        /// This method is useful to guarantee that a thread is no longer active, so that it can be garbage collected.
        /// If the waitForTerminated flag is set true, this method must not be called from its thread of execution.
        /// </remarks>
        public virtual void terminate(bool waitForTerminated)
        {
            lock (threadMutex)
            {
                if (TerminationStatus != NSFThreadTerminationStatus.ThreadTerminated)
                {
                    TerminationStatus = NSFThreadTerminationStatus.ThreadTerminating;
                }
            }

            if (waitForTerminated)
            {
                for (uint i = 0; i < TerminationTimeout; i += TerminationSleepTime)
                {
                    if (TerminationStatus == NSFThreadTerminationStatus.ThreadTerminated)
                    {
                        return;
                    }

                    NSFOSThread.sleep(TerminationSleepTime);
                }

                handleException(new Exception("Thread was unable to terminate"));
            }
        }
Beispiel #2
0
        public void terminate(bool waitForTerminated)
        {
            if (!isTopStateMachine())
            {
                TopStateMachine.terminate(waitForTerminated);
                return;
            }

            queueEvent(terminateEvent);

            if (waitForTerminated)
            {
                for (uint i = 0; i < TerminationTimeout; i += TerminationSleepTime)
                {
                    if (TerminationStatus == NSFEventHandlerTerminationStatus.EventHandlerTerminated)
                    {
                        return;
                    }

                    NSFOSThread.sleep(TerminationSleepTime);
                }

                handleException(new Exception("State machine was unable to terminate"));
            }
        }