anchor.diff:    while LoopRunning do
     begin
       TMonitor.Enter(fTaskQueue);
  -    if fTaskQueue.Count > 0 then
  -    begin
  -      Job := fTaskQueue.Dequeue;
  -    end
  -    else
  -    begin
  -      //We may only terminate once we have finished all our work
  -      if Terminated then
  +    try
  +      if fTaskQueue.Count > 0 then
         begin
  -        LoopRunning := False;
  +        Job := fTaskQueue.Dequeue;
         end
         else
         begin
  -        //Notify main thread that worker is idle if it's blocked in WaitForAllWorkToComplete
  -        fWorkCompleted := True;
  -        TMonitor.Pulse(fTaskQueue);
  +        //We may only terminate once we have finished all our work
  +        if Terminated then
  +        begin
  +          LoopRunning := False;
  +        end
  +        else
  +        begin
  +          //Notify main thread that worker is idle if it's blocked in WaitForAllWorkToComplete
  +          fWorkCompleted := True;
  +          TMonitor.Pulse(fTaskQueue);
   
  -        TMonitor.Wait(fTaskQueue, 10000);
  -        if fTaskQueue.Count > 0 then
  -          Job := fTaskQueue.Dequeue;
  +          TMonitor.Wait(fTaskQueue, 10000);
  +          if fTaskQueue.Count > 0 then
  +            Job := fTaskQueue.Dequeue;
  +        end;
         end;
  +    finally
  +      TMonitor.Exit(fTaskQueue);
       end;
  -    TMonitor.Exit(fTaskQueue);
   
       if Job <> nil then
       begin
  
positive.code:     Job.Proc := aProc;
  
      TMonitor.Enter(fTaskQueue);
  
      fWorkCompleted := False;
      fTaskQueue.Enqueue(Job);
  
      TMonitor.Pulse(fTaskQueue);
      TMonitor.Exit(fTaskQueue);
    end;
  end;
  
  
negative.code:     Exit;
  
    TMonitor.Enter(fTaskQueue);
    try
      if not fWorkCompleted and not Finished then
      begin
        if not TMonitor.Wait(fTaskQueue, 10000) then
          raise Exception.Create('Timeout in TKMWorkerThread.WaitForAllWorkToComplete');
      end;
    finally
      TMonitor.Exit(fTaskQueue);
    end;
  end;
  
  end.
  