Esempio n. 1
0
        /// <summary>
        /// Empties the queue of work items and abort the threads in the pool.
        /// </summary>
        public void Shutdown(bool forceAbort, int millisecondsTimeout)
        {
            this.ValidateNotDisposed();
            ISTPInstancePerformanceCounters counters = this._pcs;

            if (NullSTPInstancePerformanceCounters.Instance != this._pcs)
            {
                this._pcs.Dispose();
                this._pcs = NullSTPInstancePerformanceCounters.Instance;
            }
            Thread[] array = null;
            lock (this._workerThreads.SyncRoot)
            {
                this._workItemsQueue.Dispose();
                this._shutdown = true;
                this._shuttingDownEvent.Set();
                array = new Thread[this._workerThreads.Count];
                this._workerThreads.Keys.CopyTo(array, 0);
            }
            int      num   = millisecondsTimeout;
            DateTime now   = DateTime.Now;
            bool     flag  = -1 == millisecondsTimeout;
            bool     flag2 = false;

            foreach (Thread thread in array)
            {
                if (!flag && (num < 0))
                {
                    flag2 = true;
                    break;
                }
                if (!thread.Join(num))
                {
                    flag2 = true;
                    break;
                }
                if (!flag)
                {
                    TimeSpan span = (TimeSpan)(DateTime.Now - now);
                    num = millisecondsTimeout - ((int)span.TotalMilliseconds);
                }
            }
            if (flag2 && forceAbort)
            {
                foreach (Thread thread2 in array)
                {
                    if ((thread2 != null) && thread2.IsAlive)
                    {
                        try
                        {
                            thread2.Abort("Shutdown");
                        }
                        catch (SecurityException exception)
                        {
                            exception.GetHashCode();
                        }
                        catch (ThreadStateException exception2)
                        {
                            exception2.GetHashCode();
                        }
                    }
                }
            }
            counters.Dispose();
        }
Esempio n. 2
0
        /// <summary>
        /// Empties the queue of work items and abort the threads in the pool.
        /// </summary>
        private void Shutdown(bool forceAbort, int millisecondsTimeout)
        {
            ValidateNotDisposed();

            ISTPInstancePerformanceCounters pcs = _windowsPCs;

            if (NullSTPInstancePerformanceCounters.Instance != _windowsPCs)
            {
                // Set the _pcs to "null" to stop updating the performance
                // counters
                _windowsPCs = NullSTPInstancePerformanceCounters.Instance;

                pcs.Dispose();
            }

            Thread[] threads;
            lock (_workerThreads.SyncRoot)
            {
                // Shutdown the work items queue
                _workItemsQueue.Dispose();

                // Signal the threads to exit
                _shutdown = true;
                _shuttingDownEvent.Set();

                // Make a copy of the threads' references in the pool
                threads = new Thread[_workerThreads.Count];
                _workerThreads.Keys.CopyTo(threads, 0);
            }

            int       millisecondsLeft = millisecondsTimeout;
            Stopwatch stopwatch        = Stopwatch.StartNew();
            //DateTime start = DateTime.UtcNow;
            bool waitInfinitely = (Timeout.Infinite == millisecondsTimeout);
            bool timeout        = false;

            // Each iteration we update the time left for the timeout.
            foreach (Thread thread in threads)
            {
                // Join don't work with negative numbers
                if (!waitInfinitely && (millisecondsLeft < 0))
                {
                    timeout = true;
                    break;
                }

                // Wait for the thread to terminate
                bool success = thread.Join(millisecondsLeft);
                if (!success)
                {
                    timeout = true;
                    break;
                }

                if (!waitInfinitely)
                {
                    // Update the time left to wait
                    //TimeSpan ts = DateTime.UtcNow - start;
                    millisecondsLeft = millisecondsTimeout - (int)stopwatch.ElapsedMilliseconds;
                }
            }

            if (timeout && forceAbort)
            {
                // Abort the threads in the pool
                foreach (Thread thread in threads)
                {
                    if ((thread != null) &&
                        thread.IsAlive
                        )
                    {
                        try
                        {
                            thread.Abort(); // Shutdown
                        }
                        catch (SecurityException e)
                        {
                            e.GetHashCode();
                        }
                        catch (ThreadStateException ex)
                        {
                            ex.GetHashCode();
                            // In case the thread has been terminated
                            // after the check if it is alive.
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        public void Shutdown(bool forceAbort, int millisecondsTimeout)
        {
            this.ValidateNotDisposed();
            ISTPInstancePerformanceCounters windowsPCs = this._windowsPCs;

            if (NullSTPInstancePerformanceCounters.Instance != this._windowsPCs)
            {
                this._windowsPCs = NullSTPInstancePerformanceCounters.Instance;
                windowsPCs.Dispose();
            }
            object syncRoot;

            Monitor.Enter(syncRoot = this._workerThreads.SyncRoot);
            Thread[] array;
            try
            {
                this._workItemsQueue.Dispose();
                this._shutdown = true;
                this._shuttingDownEvent.Set();
                array = new Thread[this._workerThreads.Count];
                this._workerThreads.Keys.CopyTo(array, 0);
            }
            finally
            {
                Monitor.Exit(syncRoot);
            }
            int num = millisecondsTimeout;

            System.Diagnostics.Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew();
            bool flag  = -1 == millisecondsTimeout;
            bool flag2 = false;

            Thread[] array2 = array;
            for (int i = 0; i < array2.Length; i++)
            {
                Thread thread = array2[i];
                if (!flag && num < 0)
                {
                    flag2 = true;
                    break;
                }
                if (!thread.Join(num))
                {
                    flag2 = true;
                    break;
                }
                if (!flag)
                {
                    num = millisecondsTimeout - (int)stopwatch.ElapsedMilliseconds;
                }
            }
            if (flag2 && forceAbort)
            {
                Thread[] array3 = array;
                for (int j = 0; j < array3.Length; j++)
                {
                    Thread thread2 = array3[j];
                    if (thread2 != null && thread2.IsAlive)
                    {
                        try
                        {
                            thread2.Abort();
                        }
                        catch (SecurityException ex)
                        {
                            ex.GetHashCode();
                        }
                        catch (ThreadStateException ex2)
                        {
                            ex2.GetHashCode();
                        }
                    }
                }
            }
        }