Maintains a reference to all managed threads
Example #1
0
        /// <summary>
        /// Causes a thread to be scheduled for execution.
        /// </summary>
        public void Start()
        {
            if (m_type == ThreadType.QueuedThread)
            {
                throw new InvalidOperationException("Cannot manually start a thread that was queued into thread pool");
            }

            ManagedThreads.Add(this);

            m_thread.Start();
        }
Example #2
0
        private static void HandleItem(object state)
        {
            // Get next queued item
            ManagedThread item = ManagedThreads.Pop();

            // Execute callback...
            if ((object)item != null)
            {
                item.HandleItem();
            }
        }
Example #3
0
        /// <summary>
        /// Queues a work item for processing on the managed thread pool
        /// </summary>
        /// <param name="callback">A WaitCallback representing the method to execute.</param>
        /// <param name="state">An object containing data to be used by the method.</param>
        /// <returns>Reference to queued thread</returns>
        /// <remarks>
        /// This differs from the normal thread pool QueueUserWorkItem function in that it does
        /// not return a success value determing if item was queued, but rather a reference to
        /// to the managed thread that was actually placed on the queue.
        /// </remarks>
        public static ManagedThread QueueUserWorkItem(ParameterizedThreadStart callback, object state)
        {
            if ((object)callback == null)
            {
                throw (new ArgumentNullException("callback"));
            }

            ManagedThread item = new ManagedThread(ThreadType.QueuedThread, callback, state, null);

            ManagedThreads.Queue(item);

            ThreadPool.QueueUserWorkItem(HandleItem);

            return(item);
        }
Example #4
0
        /// <summary>
        /// Queues a work item for processing on the managed thread pool
        /// </summary>
        /// <param name="callback">A WaitCallback representing the method to execute.</param>
        /// <param name="state">An object containing data to be used by the method.</param>
        /// <param name="ctx">Alternate execution context in which to run the thread.</param>
        /// <returns>Reference to queued thread</returns>
        /// <remarks>
        /// This differs from the normal thread pool QueueUserWorkItem function in that it does
        /// not return a success value determing if item was queued, but rather a reference to
        /// to the managed thread that was actually placed on the queue.
        /// </remarks>
        public static ManagedThread QueueUserWorkItem(ContextCallback callback, object state, ExecutionContext ctx)
        {
            if ((object)callback == null)
            {
                throw (new ArgumentNullException("callback"));
            }

            ManagedThread item = new ManagedThread(ThreadType.QueuedThread, callback, state, ctx);

            ManagedThreads.Queue(item);

            ThreadPool.QueueUserWorkItem(HandleItem);

            return(item);
        }
Example #5
0
        /// <summary>
        /// Queues a work item for processing on the managed thread pool
        /// </summary>
        /// <param name="callback">A WaitCallback representing the method to execute.</param>
        /// <returns>Reference to queued thread</returns>
        /// <remarks>
        /// This differs from the normal thread pool QueueUserWorkItem function in that it does
        /// not return a success value determing if item was queued, but rather a reference to
        /// to the managed thread that was actually placed on the queue.
        /// </remarks>
        public static ManagedThread QueueUserWorkItem(ThreadStart callback)
        {
            if ((object)callback == null)
            {
                throw (new ArgumentNullException(nameof(callback)));
            }

            ManagedThread item = new ManagedThread(ThreadType.QueuedThread, callback, null, null);

            ManagedThreads.Queue(item);

            ThreadPool.QueueUserWorkItem(HandleItem);

            return(item);
        }
Example #6
0
        internal void HandleItem()
        {
            // Set start state
            m_startTime = DateTime.UtcNow.Ticks;
            m_status    = ThreadStatus.Executing;

            try
            {
                // Invoke the user's call back function
                if ((object)m_ctx == null)
                {
                    if ((object)m_tsCallback != null)
                    {
                        m_tsCallback.Invoke();
                    }
                    else if ((object)m_ptsCallback != null)
                    {
                        m_ptsCallback.Invoke(m_state);
                    }
                    else
                    {
                        m_ctxCallback.Invoke(m_state);
                    }
                }
#if !MONO
                else
                {
                    // If user specified an alternate execution context, we invoke
                    // their delegate under that context
                    ExecutionContext.Run(m_ctx, m_ctxCallback, m_state);
                }
#endif
            }
            finally
            {
                // Set finish state
                if (m_status == ThreadStatus.Executing)
                {
                    m_status = ThreadStatus.Completed;
                }
                m_stopTime = DateTime.UtcNow.Ticks;

                ManagedThreads.Remove(this);
            }
        }
Example #7
0
 /// <summary>
 /// Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread. Calling this method usually terminates the thread.
 /// </summary>
 /// <param name="stateInfo">An object that contains application-specific information, such as state, which can be used by the thread being aborted.</param>
 public void Abort(object stateInfo)
 {
     ManagedThreads.Cancel(this, true, stateInfo);
 }
Example #8
0
 /// <summary>
 /// Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread. Calling this method usually terminates the thread.
 /// </summary>
 public void Abort()
 {
     ManagedThreads.Cancel(this, true, null);
 }