Index: src/Common/DelftTools.Utils/Threading/ThreadedWorker.cs =================================================================== diff -u -r8f6ae890fed8e8eae3a32f9c0498a10f82e0ddf9 -r5fc71a385897af92ccb092f2f969b5709afab85a --- src/Common/DelftTools.Utils/Threading/ThreadedWorker.cs (.../ThreadedWorker.cs) (revision 8f6ae890fed8e8eae3a32f9c0498a10f82e0ddf9) +++ src/Common/DelftTools.Utils/Threading/ThreadedWorker.cs (.../ThreadedWorker.cs) (revision 5fc71a385897af92ccb092f2f969b5709afab85a) @@ -6,52 +6,39 @@ public class ThreadedWorker { private readonly ManualResetEvent allDone = new ManualResetEvent(false); - public static Action WaitMethod { get; set; } - public Action InstanceWaitMethod { get; set; } private int itemsTodo; - private Action EffectiveWaitMethod + public ThreadedWorker(bool useGlobalWaitMethod = true) { - get { return InstanceWaitMethod ?? WaitMethod; } - } - - public ThreadedWorker(bool useGlobalWaitMethod=true) - { if (!useGlobalWaitMethod) + { InstanceWaitMethod = () => { }; - + } + Reset(); } - private void Reset() - { - allDone.Reset(); - itemsTodo = 1; //1 + public static Action WaitMethod { get; set; } + public Action InstanceWaitMethod { get; set; } - //fake work item to make sure: - //--we don't get at 0 before we're done adding items - //--we don't deadlock if no items were added before wait - //---(typical reason why no items are added is if the enumeration over which it is called is empty) - } - public void ProcessWorkItemAsync(Action workItem) { Interlocked.Increment(ref itemsTodo); ThreadPool.QueueUserWorkItem( s => + { + try { - try - { - workItem(); - } - finally - { - ReportWorkItemDone(); - } + workItem(); } + finally + { + ReportWorkItemDone(); + } + } ); } - + public void WaitTillAllWorkItemsDone() { ReportWorkItemDone(); //report fake work item done (to prevent we get at 0 before we're done) @@ -61,6 +48,25 @@ Reset(); } + private Action EffectiveWaitMethod + { + get + { + return InstanceWaitMethod ?? WaitMethod; + } + } + + private void Reset() + { + allDone.Reset(); + itemsTodo = 1; //1 + + //fake work item to make sure: + //--we don't get at 0 before we're done adding items + //--we don't deadlock if no items were added before wait + //---(typical reason why no items are added is if the enumeration over which it is called is empty) + } + private void ReportWorkItemDone() { if (Interlocked.Decrement(ref itemsTodo) == 0)