public void RemoveTestRemoveExistingActiveThreads()
        {
            ThreadCollection tc = new ThreadCollection();
            GThreadMock      threadToRemove1 = new GThreadMock();
            GThreadMock      threadToRemove2 = new GThreadMock();
            GThreadMock      threadToRemove3 = new GThreadMock();
            GThreadMock      threadToRemove4 = new GThreadMock();

            tc.Add(threadToRemove1);
            tc.Add(threadToRemove2);
            tc.Add(threadToRemove3);
            tc.Add(threadToRemove4);

            Assert.AreEqual(4, tc.Count);

            threadToRemove1.SetState(ThreadState.Ready);
            threadToRemove2.SetState(ThreadState.Scheduled);
            threadToRemove3.SetState(ThreadState.Started);
            threadToRemove4.SetState(ThreadState.Unknown);

            tc.Remove(threadToRemove1);
            tc.Remove(threadToRemove2);
            tc.Remove(threadToRemove3);
            tc.Remove(threadToRemove4);

            // the thread should not have been removed
            // this remove function only allows the removal of Dead or Finished threads
            Assert.AreEqual(4, tc.Count);
        }
        public void RemoveTestNullThreadToRemove()
        {
            ThreadCollection tc             = new ThreadCollection();
            GThreadMock      threadToRemove = null;

            tc.Remove(threadToRemove);

            // if we get this far then there was no exception so we are happy
            Assert.IsTrue(true);
        }
        public void RemoveTestRemoveExistingDeadOrFinishedThread()
        {
            ThreadCollection tc = new ThreadCollection();
            GThreadMock      threadToRemove1 = new GThreadMock();
            GThreadMock      threadToRemove2 = new GThreadMock();

            tc.Add(threadToRemove1);
            tc.Add(threadToRemove2);

            Assert.AreEqual(2, tc.Count);

            threadToRemove1.SetState(ThreadState.Dead);
            threadToRemove2.SetState(ThreadState.Finished);

            tc.Remove(threadToRemove1);

            // the thread should have been removed
            Assert.AreEqual(1, tc.Count);

            tc.Remove(threadToRemove2);

            // the thread should have been removed
            Assert.AreEqual(0, tc.Count);
        }
Esempio n. 4
0
        private void StartThread(ThreadItem task)
        {
            _threads.Add(task);
            BackgroundWorker thread = new BackgroundWorker();

            thread.DoWork += delegate
            {
                task.Invoke();
            };
            thread.RunWorkerCompleted += delegate
            {
                _threads.Remove(task);

                CheckQueue();

                if (_queue.Count == 0 && _threads.Count == 0 && RaiseCompleteEventIfQueueEmpty)
                {
                    OnCompleted();
                }
            };
            thread.RunWorkerAsync();
        }