public void Worker_Threads_Stop_On_Dispose() { long count = 0; WorkerThreads threads = new WorkerThreads(TimeSpan.FromSeconds(5), (ts, obj) => { Interlocked.Increment(ref count); Thread.Sleep(100); }); try { int index = threads.AddWorker(); // pause a bit and make sure thread is working Thread.Sleep(1000); Assert.AreEqual(1, threads.Count); long curCount = Interlocked.Read(ref count); Assert.Greater(count, 0); threads.Dispose(); // pause a bit and make sure thread is actually dead long countAfterStop = Interlocked.Read(ref count); Thread.Sleep(1000); curCount = Interlocked.Read(ref count); Assert.AreEqual(curCount, countAfterStop); } finally { threads.Dispose(); } }
protected override void Dispose(bool disposing) { if (disposing) { if (_workers != null) { _workers.Dispose(); } } base.Dispose(disposing); }