示例#1
0
        /// <summary>
        /// Stops a thread by aborting it if configured to do; otherwise it will wait (forever if needed) until the thread dies.
        /// </summary>
        /// <param name="workerThread">The worker thread.</param>
        /// <returns></returns>
        public bool TryForceTerminate(Thread workerThread)
        {
            if (_abortWorkerThread.Abort(workerThread))
            {
                return(true);
            }

            //wait for the thread to exit
            _waitForThreadToFinish.Wait(workerThread);

            return(true);
        }
        /// <summary>
        /// Aborts the specified worker thread, if configured to do so.
        /// </summary>
        /// <param name="workerThread">The worker thread.</param>
        /// <returns>
        /// True if thread aborted; false otherwise
        /// </returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool Abort(Thread workerThread)
        {
            //log a warning message if we are in async mode, and the abort flag is true
            if (_messageMode.Mode == MessageProcessingModes.Async && _configuration.AbortWorkerThreadsWhenStopping)
            {
                _log.LogWarning(
                    "AbortWorkerThreadsWhenStopping is true, but we are running in async mode. Async threads cannot be aborted");
            }

            var aborted = _handler.Abort(workerThread);

            if (aborted)
            {
                _log.LogWarning(
                    $"Worker thread {workerThread.Name} was aborted due to not responding to a stop and a cancel request");
            }
            return(aborted);
        }