public void DontDisposeCallerState() { SmartThreadPool smartThreadPool = new SmartThreadPool(); 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 DisposeCallerState() { SmartThreadPool smartThreadPool = new SmartThreadPool(); 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(); }