public void WaitForIdleOnSTPThreadForAnotherWorkItemsGroup() { STP smartThreadPool = new STP(10 * 1000, 25, 0); IWorkItemsGroup workItemsGroup1 = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); IWorkItemsGroup workItemsGroup2 = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); workItemsGroup1.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), 1000); workItemsGroup1.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), 1000); IWorkItemResult wir = workItemsGroup2.QueueWorkItem( new WorkItemCallback(this.DoWaitForIdle), workItemsGroup1); Exception e; wir.GetResult(out e); smartThreadPool.Shutdown(); Assert.IsNull(e); }
/// <summary> /// Example of how to use the post execute callback /// </summary> private bool DoTestDefaultPostExecute(CallToPostExecute callToPostExecute, bool answer) { STPStartInfo stpStartInfo = new STPStartInfo { CallToPostExecute = callToPostExecute, PostExecuteWorkItemCallback = new PostExecuteWorkItemCallback(this.DoSomePostExecuteWork) }; STP smartThreadPool = new STP(stpStartInfo); bool success = false; PostExecuteResult postExecuteResult = new PostExecuteResult(); IWorkItemResult wir = smartThreadPool.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), postExecuteResult); if (!wir.IsCompleted) { int result = (int)wir.GetResult(); success = (1 == result); success = success && (postExecuteResult.wh.WaitOne(1000, true) == answer); } smartThreadPool.Shutdown(); return(success); }
/// <summary> /// Example of how to use the post execute callback /// </summary> private bool DoTestPostExecute(CallToPostExecute callToPostExecute, bool answer) { STP smartThreadPool = new STP(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); bool success = false; PostExecuteResult postExecuteResult = new PostExecuteResult(); IWorkItemResult wir = workItemsGroup.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), postExecuteResult, new PostExecuteWorkItemCallback(this.DoSomePostExecuteWork), callToPostExecute); if (!wir.IsCompleted) { int result = (int)wir.GetResult(); success = (1 == result); success = success && (postExecuteResult.wh.WaitOne(1000, true) == answer); } smartThreadPool.Shutdown(); return(success); }
public void DontDisposeCallerState() { STP smartThreadPool = new STP(); WIGStartInfo wigStartInfo = new WIGStartInfo(); wigStartInfo.DisposeOfStateObjects = false; IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue, wigStartInfo); CallerState nonDisposableCallerState = new NonDisposableCallerState(); CallerState disposableCallerState = new DisposableCallerState(); IWorkItemResult wir1 = workItemsGroup.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), nonDisposableCallerState); IWorkItemResult wir2 = workItemsGroup.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), disposableCallerState); wir1.GetResult(); bool success = (1 == nonDisposableCallerState.Value); wir2.GetResult(); success = success && (1 == disposableCallerState.Value); smartThreadPool.Shutdown(); Assert.IsTrue(success); }
public void TestWIGConcurrencyChange() { STP smartThreadPool = new STP(10 * 1000, 25, 0); IWorkItemsGroup wig = smartThreadPool.CreateWorkItemsGroup(smartThreadPool.MaxThreads); bool success = false; for (int i = 0; i < 100; ++i) { wig.QueueWorkItem(new WorkItemCallback(this.DoSomeLongWork), null); } wig.Concurrency = 1; success = WaitForWIGThreadsInUse(wig, 1, 1 * 1000); Assert.IsTrue(success); wig.Concurrency = 5; success = WaitForWIGThreadsInUse(wig, 5, 2 * 1000); Assert.IsTrue(success); wig.Concurrency = 25; success = WaitForWIGThreadsInUse(wig, 25, 4 * 1000); Assert.IsTrue(success); wig.Concurrency = 10; success = WaitForWIGThreadsInUse(wig, 10, 10 * 1000); Assert.IsTrue(success); smartThreadPool.Shutdown(); }
public void WorkItemWaitCanceling() { Assert.ThrowsException <WorkItemTimeoutException>(() => { STP smartThreadPool = new STP(); ManualResetEvent cancelWaitHandle = new ManualResetEvent(false); // Queue a work item that will occupy the thread in the pool IWorkItemResult wir1 = smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); // Queue another work item that will wait for the first to complete IWorkItemResult wir2 = smartThreadPool.QueueWorkItem(new WorkItemCallback(this.SignalCancel), cancelWaitHandle); try { wir1.GetResult(System.Threading.Timeout.Infinite, true, cancelWaitHandle); } finally { smartThreadPool.Shutdown(); } }); }
public void WorkItemWaitCanceling() { STP smartThreadPool = new STP(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); ManualResetEvent cancelWaitHandle = new ManualResetEvent(false); bool success = false; // Queue a work item that will occupy the thread in the pool IWorkItemResult wir1 = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); // Queue another work item that will wait for the first to complete IWorkItemResult wir2 = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.SignalCancel), cancelWaitHandle); try { wir1.GetResult(System.Threading.Timeout.Infinite, true, cancelWaitHandle); } catch (WorkItemTimeoutException) { success = true; } smartThreadPool.Shutdown(); Assert.IsTrue(success); }
public void WaitAny() { STP smartThreadPool = new STP(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); bool success = false; IWorkItemResult[] wirs = new IWorkItemResult[5]; for (int i = 0; i < wirs.Length; ++i) { wirs[i] = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); } int index = STP.WaitAny(wirs); if (wirs[index].IsCompleted) { int result = (int)wirs[index].GetResult(); if (1 == result) { success = true; } } smartThreadPool.Shutdown(); Assert.IsTrue(success); }
public void TestWIGConcurrencyChange2WIGs() { STP smartThreadPool = new STP(10 * 1000, 2, 0); Assert.IsTrue(0 == smartThreadPool.WaitingCallbacks); PauseSTP(smartThreadPool); PauseSTP(smartThreadPool); Thread.Sleep(100); Assert.IsTrue(0 == smartThreadPool.WaitingCallbacks); IWorkItemsGroup wig1 = smartThreadPool.CreateWorkItemsGroup(1); IWorkItemsGroup wig2 = smartThreadPool.CreateWorkItemsGroup(1); wig1.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); Assert.IsTrue(1 == smartThreadPool.WaitingCallbacks); wig2.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); Assert.IsTrue(2 == smartThreadPool.WaitingCallbacks); wig1.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); Assert.IsTrue(2 == smartThreadPool.WaitingCallbacks); wig2.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); Assert.IsTrue(2 == smartThreadPool.WaitingCallbacks); wig1.Concurrency = 2; Thread.Sleep(100); Assert.IsTrue(3 == smartThreadPool.WaitingCallbacks); wig2.Concurrency = 2; Thread.Sleep(100); Assert.IsTrue(4 == smartThreadPool.WaitingCallbacks); ResumeSTP(smartThreadPool); Thread.Sleep(100); Assert.IsTrue(0 == smartThreadPool.WaitingCallbacks); PauseSTP(smartThreadPool); PauseSTP(smartThreadPool); Thread.Sleep(100); wig1.Concurrency = 1; wig1.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); Assert.IsTrue(1 == smartThreadPool.WaitingCallbacks); wig2.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); Assert.IsTrue(2 == smartThreadPool.WaitingCallbacks); ResumeSTP(smartThreadPool); Thread.Sleep(100); Assert.IsTrue(0 == smartThreadPool.WaitingCallbacks); smartThreadPool.Shutdown(); }
public void GoodCallback() { STP smartThreadPool = new STP(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); workItemsGroup.QueueWorkItem(new WorkItemCallback(DoWork)); workItemsGroup.WaitForIdle(); smartThreadPool.Shutdown(); }
public void WaitForIdleEvent() { STP smartThreadPool = new STP(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(1); ManualResetEvent wigIsIdle = new ManualResetEvent(false); workItemsGroup.OnIdle += wig => wigIsIdle.Set(); workItemsGroup.QueueWorkItem(() => { }); bool eventFired = wigIsIdle.WaitOne(100, true); smartThreadPool.Shutdown(); Assert.IsTrue(eventFired); }
public void ChainedDelegatesCallback() { Assert.ThrowsException <NotSupportedException>(() => { STP smartThreadPool = new STP(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); WorkItemCallback workItemCallback = new WorkItemCallback(DoWork); workItemCallback += new WorkItemCallback(DoWork); workItemsGroup.QueueWorkItem(workItemCallback); workItemsGroup.WaitForIdle(); smartThreadPool.Shutdown(); }); }
private void Concurrency( int concurrencyPerWig, int wigsCount, int workItemsCount) { Console.WriteLine( "Testing : concurrencyPerWig = {0}, wigsCount = {1}, workItemsCount = {2}", concurrencyPerWig, wigsCount, workItemsCount); _success = true; _concurrencyPerWig = concurrencyPerWig; _randGen = new Random(0); STPStartInfo stpStartInfo = new STPStartInfo { StartSuspended = true }; STP stp = new STP(stpStartInfo); _concurrentOps = new int[wigsCount]; IWorkItemsGroup[] wigs = new IWorkItemsGroup[wigsCount]; for (int i = 0; i < wigs.Length; ++i) { wigs[i] = stp.CreateWorkItemsGroup(_concurrencyPerWig); for (int j = 0; j < workItemsCount; ++j) { wigs[i].QueueWorkItem(new WorkItemCallback(this.DoWork), i); } wigs[i].Start(); } stp.Start(); stp.WaitForIdle(); Assert.IsTrue(_success); stp.Shutdown(); }
public void WaitForIdleWithCancel() { STP smartThreadPool = new STP(10 * 1000, 1, 1); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(2); _x = 0; IWorkItemResult wir1 = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), 1000); IWorkItemResult wir2 = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), 1000); IWorkItemResult wir3 = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), 1000); while (0 == _x) { Thread.Sleep(10); } Console.WriteLine("{0}: Cancelling WIG", DateTime.Now.ToLongTimeString()); workItemsGroup.Cancel(); // At this point: // The first work item is running // The second work item is cancelled, but waits in the STP queue // The third work item is cancelled. Assert.AreEqual(1, _x); Assert.IsTrue(wir2.IsCanceled); Assert.IsTrue(wir3.IsCanceled); // Make sure the workItemsGroup is still idle Assert.IsFalse(workItemsGroup.IsIdle); Console.WriteLine("{0}: Waiting for 1st result", DateTime.Now.ToLongTimeString()); wir1.GetResult(); Assert.AreEqual(2, _x); bool isIdle = workItemsGroup.WaitForIdle(100); Assert.IsTrue(isIdle); smartThreadPool.Shutdown(); }
public void BlockingCall() { STP smartThreadPool = new STP(); bool success = false; IWorkItemResult wir = smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); if (!wir.IsCompleted) { int result = (int)wir.GetResult(); success = (1 == result); } smartThreadPool.Shutdown(); Assert.IsTrue(success); }
public void Timeout() { Assert.ThrowsException <WorkItemTimeoutException>(() => { STP smartThreadPool = new STP(); IWorkItemResult wir = smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); try { wir.GetResult(500, true); } finally { smartThreadPool.Shutdown(); } }); }
public void WaitAllWithTimeoutFailure() { STP smartThreadPool = new STP(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); IWorkItemResult[] wirs = new IWorkItemResult[5]; for (int i = 0; i < wirs.Length; ++i) { wirs[i] = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); } bool timeout = !STP.WaitAll(wirs, 10, true); bool success = timeout; smartThreadPool.Shutdown(); Assert.IsTrue(success); }
public void WaitForIdle() { STP smartThreadPool = new STP(10 * 1000, 25, 0); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); ManualResetEvent isRunning = new ManualResetEvent(false); for (int i = 0; i < 4; ++i) { workItemsGroup.QueueWorkItem(delegate { isRunning.WaitOne(); }); } bool success = !workItemsGroup.WaitForIdle(1000); isRunning.Set(); success = success && workItemsGroup.WaitForIdle(1000); smartThreadPool.Shutdown(); Assert.IsTrue(success); }
public void WaitAnyWithTimeoutSuccess() { STP smartThreadPool = new STP(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); bool success; IWorkItemResult[] wirs = new IWorkItemResult[5]; for (int i = 0; i < wirs.Length; ++i) { wirs[i] = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); } int index = STP.WaitAny(wirs, 1500, true); success = (index != WaitHandle.WaitTimeout); smartThreadPool.Shutdown(); Assert.IsTrue(success); }
public void Timeout() { STP smartThreadPool = new STP(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); bool success = false; IWorkItemResult wir = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); try { wir.GetResult(500, true); } catch (WorkItemTimeoutException) { success = true; } smartThreadPool.Shutdown(); Assert.IsTrue(success); }
/// <summary> /// Example of how to queue a work item and then cancel it while it is in the queue. /// </summary> private bool DoTestPostExecuteWithCancel(CallToPostExecute callToPostExecute, bool answer) { // Create a STP with only one thread. // It just to show how to use the work item canceling feature STP smartThreadPool = new STP(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(1); bool success = false; PostExecuteResult postExecuteResult = new PostExecuteResult(); // Queue a work item that will occupy the thread in the pool workItemsGroup.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), null); // Queue another work item that will wait for the first to complete IWorkItemResult wir = workItemsGroup.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), postExecuteResult, new PostExecuteWorkItemCallback(this.DoSomePostExecuteWork), callToPostExecute); // Wait a while for the thread pool to start executing the first work item Thread.Sleep(100); // Cancel the second work item while it still in the queue if (wir.Cancel()) { success = (postExecuteResult.wh.WaitOne(1000, true) == answer); } smartThreadPool.Shutdown(); return(success); }
public void DisposeCallerState() { STP smartThreadPool = new STP(); WIGStartInfo wigStartInfo = new WIGStartInfo(); wigStartInfo.DisposeOfStateObjects = true; IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue, wigStartInfo); CallerState nonDisposableCallerState = new NonDisposableCallerState(); CallerState disposableCallerState = new DisposableCallerState(); IWorkItemResult wir1 = workItemsGroup.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), nonDisposableCallerState); IWorkItemResult wir2 = workItemsGroup.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), disposableCallerState); wir1.GetResult(); Assert.AreEqual(1, nonDisposableCallerState.Value); wir2.GetResult(); // Wait a little bit for the working thread to call dispose on the // work item's state. workItemsGroup.WaitForIdle(); Assert.AreEqual(2, disposableCallerState.Value); smartThreadPool.Shutdown(); }
public void Fini() { _stp.WaitForIdle(); _stp.Shutdown(); }
public void Fini() { _stp.Shutdown(); }