Skip to content

Add ability to wait for Thread to Abort #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions SmartThreadPool/Interfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,21 @@ TResult GetResult(
/// <returns>Returns true if the work item was not completed, otherwise false.</returns>
bool Cancel(bool abortExecution);

/// <summary>
/// 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
/// </summary>
/// <param name="abortExecution">When true send an AbortException to the executing thread.</param>
/// <param name="timeToWaitForThreadAbort">Amount of time to wait for the thread to abort.</param>
/// <returns>Returns true if the work item was not completed, otherwise false.</returns>
bool Cancel(bool abortExecution, TimeSpan timeToWaitForThreadAbort);

/// <summary>
/// Get the work item's priority
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions SmartThreadPool/WorkItem.WorkItemResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions SmartThreadPool/WorkItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,15 @@ internal void WorkItemIsQueued()
/// </summary>
/// <returns>Returns true on success or false if the work item is in progress or already completed</returns>
private bool Cancel(bool abortExecution)
{
return Cancel(abortExecution, TimeSpan.Zero);
}

/// <summary>
/// Cancel the work item if it didn't start running yet.
/// </summary>
/// <returns>Returns true on success or false if the work item is in progress or already completed</returns>
private bool Cancel(bool abortExecution, TimeSpan timeToWaitForThreadAbort)
{
bool success = false;
bool signalComplete = false;
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down
5 changes: 5 additions & 0 deletions SmartThreadPool/WorkItemResultTWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down