/// <summary> /// Queue thread for processing /// </summary> /// <remarks> /// Typically only used by queued threads to add work items to the queue /// </remarks> internal static void Queue(ManagedThread item) { lock (s_queuedThreads) { s_queuedThreads.AddLast(item); } }
/// <summary> /// Remove completed thread from active thread list /// </summary> internal static void Remove(ManagedThread item) { lock (s_queuedThreads) { s_activeThreads.Remove(item); } }
/// <summary> /// Add an item to the active thread list /// </summary> /// <remarks> /// Typically only used by standard threads when user calls "Start" /// </remarks> internal static void Add(ManagedThread item) { // Standard threads are simply added to the active thread list when started lock (s_queuedThreads) { item.Status = ThreadStatus.Started; s_activeThreads.AddLast(item); } }
/// <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("callback")); ManagedThread item = new ManagedThread(ThreadType.QueuedThread, callback, null, null); ManagedThreads.Queue(item); ThreadPool.QueueUserWorkItem(HandleItem); return item; }
/// <summary> /// Removes a queued thread from thread pool if still queued, if allowAbort is True /// aborts the thread if executing (standard or queued) /// </summary> /// <param name="item">Thread to cancel</param> /// <param name="allowAbort">Set to True to abort thread if executing</param> /// <param name="stateInfo">An object that contains application-specific information, such as state, which can be used by the thread being aborted.</param> public static void Cancel(ManagedThread item, bool allowAbort, object stateInfo) { if ((object)item == null) throw new ArgumentNullException(nameof(item)); LinkedListNode<ManagedThread> node; lock (s_queuedThreads) { // Change thread status to aborted item.Status = ThreadStatus.Aborted; // See if item is still queued for execution in thread pool node = s_queuedThreads.Find(item); // Handle abort or dequeue if ((object)node == null) { if (allowAbort) { // Started items may be aborted, even if running in thread pool if ((object)stateInfo == null) item.Thread.Abort(); else item.Thread.Abort(stateInfo); } } else { // Remove item from queue if queued thread has yet to start s_queuedThreads.Remove(node); } } }
private static string ThreadStatusText(ManagedThread item) { string runtime = item.RunTime.ToString(); switch (item.Status) { case ThreadStatus.Unstarted: return "Not Started"; case ThreadStatus.Queued: return "Queued"; case ThreadStatus.Executing: return "Executing for " + runtime; case ThreadStatus.Completed: return "Completed in " + runtime; case ThreadStatus.Aborted: return "Aborted, ran for " + runtime; default: return "Status Unknown"; } }
/// <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; }
/// <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(nameof(callback))); ManagedThread item = new ManagedThread(ThreadType.QueuedThread, callback, state, null); ManagedThreads.Queue(item); ThreadPool.QueueUserWorkItem(HandleItem); return item; }