From 982ecfd18811b2b3b979d5e3531f73e6a92e06e5 Mon Sep 17 00:00:00 2001 From: Michel Zehnder Date: Fri, 17 Nov 2023 10:54:46 +0100 Subject: [PATCH 1/2] Add ability to wait for Thread to Abort --- SmartThreadPool/Interfaces.cs | 22 ++++++++++++++++++++++ SmartThreadPool/WorkItem.WorkItemResult.cs | 5 +++++ SmartThreadPool/WorkItem.cs | 15 +++++++++++++++ SmartThreadPool/WorkItemResultTWrapper.cs | 5 +++++ 4 files changed, 47 insertions(+) diff --git a/SmartThreadPool/Interfaces.cs b/SmartThreadPool/Interfaces.cs index ed9622e..10b46c3 100644 --- a/SmartThreadPool/Interfaces.cs +++ b/SmartThreadPool/Interfaces.cs @@ -117,6 +117,13 @@ public interface IWorkItemsGroup /// True to stop work items by raising ThreadAbortException void Cancel(bool abortExecution); + /// + /// Cancel all work items using thread abortion + /// + /// True to stop work items by raising ThreadAbortException + /// The amount of time to wait for a thread to abort + void Cancel(bool abortExecution, TimeSpan timeToWaitForThreadAbort); + /// /// Wait for all work item to complete. /// @@ -565,6 +572,21 @@ TResult GetResult( /// Returns true if the work item was not completed, otherwise false. bool Cancel(bool abortExecution); + /// + /// Cancel the work item execution. + /// If the work item is in the queue then it won't execute + /// If the work item is completed, it will remain completed + /// If the work item is in progress then the user can check the SmartThreadPool.IsWorkItemCanceled + /// property to check if the work item has been cancelled. If the abortExecution is set to true then + /// the Smart Thread Pool will send an AbortException to the running thread to stop the execution + /// of the work item. When an in progress work item is canceled its GetResult will throw WorkItemCancelException. + /// If the work item is already cancelled it will remain cancelled + /// + /// When true send an AbortException to the executing thread. + /// Amount of time to wait for the thread to abort. + /// Returns true if the work item was not completed, otherwise false. + bool Cancel(bool abortExecution, TimeSpan timeToWaitForThreadAbort); + /// /// Get the work item's priority /// diff --git a/SmartThreadPool/WorkItem.WorkItemResult.cs b/SmartThreadPool/WorkItem.WorkItemResult.cs index f965c92..2c641b1 100644 --- a/SmartThreadPool/WorkItem.WorkItemResult.cs +++ b/SmartThreadPool/WorkItem.WorkItemResult.cs @@ -104,6 +104,11 @@ public bool Cancel(bool abortExecution) return _workItem.Cancel(abortExecution); } + public bool Cancel(bool abortExecution, TimeSpan timeToWaitForThreadAbort) + { + return _workItem.Cancel(abortExecution, timeToWaitForThreadAbort); + } + public object State { get diff --git a/SmartThreadPool/WorkItem.cs b/SmartThreadPool/WorkItem.cs index 5cff866..2b4bfa3 100644 --- a/SmartThreadPool/WorkItem.cs +++ b/SmartThreadPool/WorkItem.cs @@ -708,6 +708,15 @@ internal void WorkItemIsQueued() /// /// Returns true on success or false if the work item is in progress or already completed private bool Cancel(bool abortExecution) + { + return Cancel(abortExecution, TimeSpan.Zero); + } + + /// + /// Cancel the work item if it didn't start running yet. + /// + /// Returns true on success or false if the work item is in progress or already completed + private bool Cancel(bool abortExecution, TimeSpan timeToWaitForThreadAbort) { bool success = false; bool signalComplete = false; @@ -727,6 +736,9 @@ private bool Cancel(bool abortExecution) // No need to signalComplete, because we already cancelled this work item // so it already signaled its completion. //signalComplete = true; + if (timeToWaitForThreadAbort > TimeSpan.Zero){ + executionThread.Join(timeToWaitForThreadAbort); + } } } success = true; @@ -741,6 +753,9 @@ private bool Cancel(bool abortExecution) if (null != executionThread) { executionThread.Abort(); // "Cancel" + if (timeToWaitForThreadAbort > TimeSpan.Zero){ + executionThread.Join(timeToWaitForThreadAbort); + } success = true; signalComplete = true; } diff --git a/SmartThreadPool/WorkItemResultTWrapper.cs b/SmartThreadPool/WorkItemResultTWrapper.cs index fbd0c8b..0a81bbe 100644 --- a/SmartThreadPool/WorkItemResultTWrapper.cs +++ b/SmartThreadPool/WorkItemResultTWrapper.cs @@ -91,6 +91,11 @@ public bool Cancel(bool abortExecution) return _workItemResult.Cancel(abortExecution); } + public bool Cancel(bool abortExecution, TimeSpan timeToWaitForThreadAbort) + { + return _workItemResult.Cancel(abortExecution, timeToWaitForThreadAbort); + } + public WorkItemPriority WorkItemPriority { get { return _workItemResult.WorkItemPriority; } From ce532c4ff17f7d20b6a14ccc028405588b848879 Mon Sep 17 00:00:00 2001 From: Michel Zehnder Date: Fri, 17 Nov 2023 20:17:09 +0100 Subject: [PATCH 2/2] Remove from WorkItemGroup --- SmartThreadPool/Interfaces.cs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/SmartThreadPool/Interfaces.cs b/SmartThreadPool/Interfaces.cs index 10b46c3..7164c2a 100644 --- a/SmartThreadPool/Interfaces.cs +++ b/SmartThreadPool/Interfaces.cs @@ -117,13 +117,6 @@ public interface IWorkItemsGroup /// True to stop work items by raising ThreadAbortException void Cancel(bool abortExecution); - /// - /// Cancel all work items using thread abortion - /// - /// True to stop work items by raising ThreadAbortException - /// The amount of time to wait for a thread to abort - void Cancel(bool abortExecution, TimeSpan timeToWaitForThreadAbort); - /// /// Wait for all work item to complete. ///