in fact there is no thread.abort

This commit is contained in:
UbitUmarov
2024-04-11 04:29:01 +01:00
parent c981f3c448
commit c0a2569b32
+28 -58
View File
@@ -799,21 +799,21 @@ namespace Amib.Threading
/// </summary> /// </summary>
public void Shutdown() public void Shutdown()
{ {
Shutdown(true, 0); Shutdown(0);
} }
/// <summary> /// <summary>
/// Force the SmartThreadPool to shutdown with timeout /// Force the SmartThreadPool to shutdown with timeout
/// </summary> /// </summary>
public void Shutdown(bool forceAbort, TimeSpan timeout) public void Shutdown(TimeSpan timeout)
{ {
Shutdown(forceAbort, (int)timeout.TotalMilliseconds); Shutdown((int)timeout.TotalMilliseconds);
} }
/// <summary> /// <summary>
/// Empties the queue of work items and abort the threads in the pool. /// Empties the queue of work items and abort the threads in the pool.
/// </summary> /// </summary>
public void Shutdown(bool forceAbort, int millisecondsTimeout) public void Shutdown(int millisecondsTimeout)
{ {
ValidateNotDisposed(); ValidateNotDisposed();
@@ -835,71 +835,41 @@ namespace Amib.Threading
int millisecondsLeft = millisecondsTimeout; int millisecondsLeft = millisecondsTimeout;
Stopwatch stopwatch = Stopwatch.StartNew(); Stopwatch stopwatch = Stopwatch.StartNew();
//DateTime start = DateTime.UtcNow; if (millisecondsLeft >= Timeout.Infinite)
bool waitInfinitely = (Timeout.Infinite == millisecondsTimeout);
bool timeout = false;
// Each iteration we update the time left for the timeout.
foreach (ThreadEntry te in threadEntries)
{ {
if (te is null)
continue;
Thread thread = te.WorkThread;
if(thread is null)
continue;
// Join don't work with negative numbers
if (!waitInfinitely && (millisecondsLeft < 0))
{
timeout = true;
break;
}
// Wait for the thread to terminate
bool success = thread.Join(millisecondsLeft);
if (!success)
{
timeout = true;
break;
}
if (!waitInfinitely)
{
// Update the time left to wait
//TimeSpan ts = DateTime.UtcNow - start;
millisecondsLeft = millisecondsTimeout - (int)stopwatch.ElapsedMilliseconds;
}
te.WorkThread = null;
}
if (timeout && forceAbort)
{
// Abort the threads in the pool
foreach (ThreadEntry te in threadEntries) foreach (ThreadEntry te in threadEntries)
{ {
if (te is null) if (te is null)
continue; continue;
Thread thread = te.WorkThread; Thread thread = te.WorkThread;
if (thread is not null && thread.IsAlive ) if(thread is null)
continue;
if (thread.IsAlive)
{ {
try // Wait for the thread to terminate
_ = thread.Join(millisecondsLeft);
if (millisecondsLeft > 0)
{ {
//thread.Abort(); // Shutdown // Update the time left to wait
te.WorkThread = null; millisecondsLeft = millisecondsTimeout - (int)stopwatch.ElapsedMilliseconds;
} if (millisecondsLeft < 0)
catch (SecurityException e) millisecondsLeft= 0;
{
e.GetHashCode();
}
catch (ThreadStateException ex)
{
ex.GetHashCode();
// In case the thread has been terminated
// after the check if it is alive.
} }
} }
te.WorkThread = null;
}
}
else
{
// there is no Abort in dotnet > 5, so we can't do anything
foreach (ThreadEntry te in threadEntries)
{
if (te is null)
continue;
te.WorkThread = null;
} }
} }
} }