public void CompositeTaskRunnerDoesntHoldLockWhileCallingIterate() { object lockObj = new object(); // Start a task running that takes a shared lock during it's iterate. CompositeTaskRunner runner = new CompositeTaskRunner(); runner.AddTask(new LockingTask(lockObj)); // Start a separate thread that holds that same lock whilst manipulating the CompositeTaskRunner (See InactivityMonitor for real example). AutoResetEvent resetEvent = new AutoResetEvent(false); ThreadPool.QueueUserWorkItem(_ => { for (int i = 0; i < 10000; i++) { lock (lockObj) { var countingTask = new CountingTask("task1", 100); runner.AddTask(countingTask); runner.RemoveTask(countingTask); } } resetEvent.Set(); }); // Wait for the second thread to finish 10000 attempts. Assert.That(resetEvent.WaitOne(TimeSpan.FromSeconds(10)), "The secondary lock user didn't finish 10000 iterations in less than 10 seconds. Probably dead locked!"); runner.Shutdown(); }
private void StopMonitorThreads() { lock (_monitor) if (_monitorStarted.CompareAndSet(true, false)) { _connectionCheckTimer.Dispose(); _connectionCheckTimer.Dispose(); _asyncTasks.Shutdown(); _asyncTasks = null; _asyncWriteTask = null; _asyncErrorTask = null; } }