diff --git a/SmartThreadPool/Interfaces.cs b/SmartThreadPool/Interfaces.cs
index ed9622e..7164c2a 100644
--- a/SmartThreadPool/Interfaces.cs
+++ b/SmartThreadPool/Interfaces.cs
@@ -565,6 +565,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; }