Skip to content

Commit

Permalink
Fixes #68, "When you cancel the crawl job, the WebCrawler will dispos…
Browse files Browse the repository at this point in the history
…e the _threadManager and its _resetEvent will also be displsed. But the ThreadManager class can't make sure that all threads running have been completed at the time. So if a thread is running the RunAction method and it will catch a OperationCanceledException exception and finally _resetEvent.Set() will throw a exception said the safe handle was closed."
  • Loading branch information
sjdirect committed Apr 1, 2015
1 parent e0cc344 commit c983870
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions Abot/Util/ThreadManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public abstract class ThreadManager : IThreadManager
protected int _numberOfRunningThreads = 0;
protected ManualResetEvent _resetEvent = new ManualResetEvent(true);
protected Object _locker = new Object();
protected bool _isDisplosed = false;

public ThreadManager(int maxThreads)
{
Expand Down Expand Up @@ -68,13 +69,13 @@ public virtual void DoWork(Action action)
if (_abortAllCalled)
throw new InvalidOperationException("Cannot call DoWork() after AbortAll() or Dispose() have been called.");

if (MaxThreads > 1)
if (!_isDisplosed && MaxThreads > 1)
{
_resetEvent.WaitOne();
lock (_locker)
{
_numberOfRunningThreads++;
if (_numberOfRunningThreads >= MaxThreads)
if (!_isDisplosed && _numberOfRunningThreads >= MaxThreads)
_resetEvent.Reset();

_logger.DebugFormat("Starting another thread, increasing running threads to [{0}].", _numberOfRunningThreads);
Expand All @@ -97,6 +98,7 @@ public virtual void Dispose()
{
AbortAll();
_resetEvent.Dispose();
_isDisplosed = true;
}

public virtual bool HasRunningThreads()
Expand Down Expand Up @@ -129,7 +131,7 @@ protected virtual void RunAction(Action action, bool decrementRunningThreadCount
{
_numberOfRunningThreads--;
_logger.DebugFormat("[{0}] threads are running.", _numberOfRunningThreads);
if (_numberOfRunningThreads < MaxThreads)
if (!_isDisplosed && _numberOfRunningThreads < MaxThreads)
_resetEvent.Set();
}
}
Expand Down

0 comments on commit c983870

Please sign in to comment.