示例#1
0
 /// <summary> Adds a unit of work to the list </summary>
 public void AddWork(WaitAndContinueList list)
 {
     if (_control.HasQuit)
     {
         throw new ObjectDisposedException(GetType().FullName);
     }
     _work.AddWork(list);
     _control.Modified();
 }
示例#2
0
        /// <summary> Constructs a thread to process IWaitAndContinue work items </summary>
        public WaitAndContinueWorker()
        {
            _control = new WorkerControl();
            _work    = new WaitAndContinueList();
            _work.AddWork(_control);

            _worker = new Thread(Run);
            _worker.SetApartmentState(ApartmentState.MTA);
            _worker.IsBackground = true;
            _worker.Name         = GetType().Name;
            _worker.Start();
        }
        /// <summary> Constructs a thread to process IWaitAndContinue work items </summary>
        public WaitAndContinueWorker()
        {
            _control = new WorkerControl();
            _work = new WaitAndContinueList();
            _work.AddWork(_control);

            _worker = new Thread(Run);
            _worker.SetApartmentState(ApartmentState.MTA);
            _worker.IsBackground = true;
            _worker.Name = GetType().Name;
            _worker.Start();
        }
 /// <summary> Moves the work in the other list into this list </summary>
 public void AddWork(WaitAndContinueList other)
 {
     if (_disposed) throw new ObjectDisposedException(GetType().FullName);
     lock (_list)
     lock (other._list)
     {
         Node next = other._list.First;
         while (next != null)
         {
             Node node = next;
             next = next.Next;
             other._list.Remove(node);
             _list.AddLast(node);
         }
     }
 }
示例#5
0
 /// <summary> Moves the work in the other list into this list </summary>
 public void AddWork(WaitAndContinueList other)
 {
     if (_disposed)
     {
         throw new ObjectDisposedException(GetType().FullName);
     }
     lock (_list)
         lock (other._list)
         {
             Node next = other._list.First;
             while (next != null)
             {
                 Node node = next;
                 next = next.Next;
                 other._list.Remove(node);
                 _list.AddLast(node);
             }
         }
 }
        private int InternalSend(int waitTime, IEnumerable<string> identities, string eventName, params string[] arguments)
        {
            int count = 0;
            using (WaitAndContinueList work = new WaitAndContinueList())
            {
                foreach (string identity in Check.NotNull(identities))
                {
                    IpcEventMessage m = new IpcEventMessage(this, ExecutionTimeout, identity, eventName, arguments);
                    if (!m.Completed)
                        work.AddWork(m);
                    else
                        count += m.Successful ? 1 : 0;
                }

                if (!work.IsEmpty)
                {
                    //Waiting while in-call results in dead-locks, so we force these to defer if they do not complete immediatly
                    if (InCall) waitTime = 0;

                    System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
                    if (waitTime > 0)
                        timer.Start();

                    IWaitAndContinue waitItem;
                    int ticksRemaining = waitTime;
                    while (work.PerformWork(ticksRemaining, out waitItem))
                    {
                        count += ((IpcEventMessage) waitItem).Successful ? 1 : 0;
                        if (waitTime > 0 && (ticksRemaining = (int) (waitTime - timer.ElapsedMilliseconds)) < 0)
                            break;
                    }

                    if (!work.IsEmpty)
                    {
                        WaitAndContinueWorker worker = _deferred;
                        if (worker != null)
                            try { worker.AddWork(work); } catch (ObjectDisposedException) { }
                    }
                }
            }
            return count;
        }
 /// <summary> Adds a unit of work to the list </summary>
 public void AddWork(WaitAndContinueList list)
 {
     if (_control.HasQuit) throw new ObjectDisposedException(GetType().FullName);
     _work.AddWork(list);
     _control.Modified();
 }
 public void TestCompletedEnqueue()
 {
     WaitAndContinueList work = new WaitAndContinueList();
     SampleWork item = new SampleWork();
     item.Completed = true;
     Assert.IsFalse(item.Disposed);
     work.AddWork(item);
     Assert.IsTrue(work.IsEmpty);
     Assert.IsTrue(item.Disposed);
 }
        public void TestPerformWork()
        {
            WaitAndContinueList work = new WaitAndContinueList();
            SampleWork item1, item2;
            IWaitAndContinue signaled;

            work.AddWork(item1 = new SampleWork());
            work.AddWork(item2 = new SampleWork());

            Assert.AreEqual(2, work.Count);
            Assert.IsFalse(work.IsEmpty);

            //normally done as a loop: while(work.Perform(timeout)) { }
            Assert.IsFalse(work.PerformWork(1));
            item2.Ready.Set();
            Assert.IsTrue(work.PerformWork(0));
            Assert.IsTrue(item2.Completed);
            Assert.IsTrue(item2.Disposed);

            Assert.IsFalse(work.PerformWork(1));
            item1.Cancel.Set();
            Assert.IsTrue(work.PerformWork(0, out signaled));
            Assert.IsTrue(ReferenceEquals(signaled, item1));
            Assert.IsTrue(item1.Completed);
            Assert.IsTrue(item1.Cancelled);
            Assert.IsTrue(item1.Disposed);

            Assert.IsTrue(work.IsEmpty);
            Assert.AreEqual(0, work.Count);
            Assert.IsFalse(work.PerformWork(1));
        }
        public void TestAddWorkListToDisposedWorker()
        {
            WaitAndContinueWorker work = new WaitAndContinueWorker();
            work.Complete(false, 100);
            work.Dispose();

            WaitAndContinueList list = new WaitAndContinueList();
            list.AddWork(new SampleWork());

            work.AddWork(list);
        }
 public void TestAddWorkListToDisposedList()
 {
     WaitAndContinueList work = new WaitAndContinueList();
     work.Dispose();
     work.AddWork(new WaitAndContinueList());
 }
        public void TestPerformDisposedWork()
        {
            WaitAndContinueList work = new WaitAndContinueList();
            SampleWork item = new SampleWork();

            Assert.IsFalse(item.Disposed);
            work.AddWork(item);

            Assert.IsFalse(work.PerformWork(0));
            item.Dispose();

            Assert.IsFalse(work.PerformWork(0));

            item.Completed = true;//Normally this would be set in the Dispose method of the WorkItem, but we are testing
            Assert.IsFalse(work.PerformWork(0));

            Assert.IsTrue(work.IsEmpty);
            Assert.IsTrue(item.Disposed);
        }
        public void TestPerformByAbandonMutex()
        {
            Mutex abandondMutex = new Mutex();
            Thread t = new Thread(delegate() { abandondMutex.WaitOne(); });
            t.Start();
            t.Join();

            WaitAndContinueList work = new WaitAndContinueList();
            SampleWork item = new SampleWork();
            item.Mutex = abandondMutex;

            Assert.IsFalse(item.Disposed);
            work.AddWork(item);
            Assert.IsTrue(work.PerformWork(0));
            Assert.IsTrue(work.IsEmpty);
            Assert.IsTrue(item.Disposed);
            Assert.IsTrue(item.MutexAcquired);
        }
        public void TestPerformWorkThrows()
        {
            WaitAndContinueList work = new WaitAndContinueList();
            SampleWork item = new SampleWork();
            item.WorkThrows = true;
            work.AddWork(item);

            Assert.IsFalse(work.IsEmpty);
            Assert.IsFalse(work.PerformWork(1));
            item.Ready.Set();
            try
            {
                work.PerformWork(0);
                Assert.Fail("Expected exception.");
            }
            catch (ArgumentOutOfRangeException ae)
            {
                Assert.AreEqual("WorkThrows", ae.ParamName);
            }
            Assert.IsTrue(work.IsEmpty);
            Assert.IsTrue(item.Disposed);
            Assert.IsFalse(item.Completed); // incomplete, but still disposed... abandond due to exception.
        }