/// <summary>
        /// Implements the entry point for the thread.
        /// </summary>
        /// <param name="context">Additional contextual information.</param>
        private void threadEntry(NSFContext context)
        {
            try
            {
                threadLoop();
            }
            catch (Exception exception)
            {
                handleException(new Exception(Name + " thread loop exception", exception));
            }

            lock (threadMutex)
            {
                TerminationStatus = NSFThreadTerminationStatus.ThreadTerminated;
                NSFEnvironment.removeThread(this);
            }
        }
        /// <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"));
            }
        }