/// <summary>
 /// Shutsdown the worker thread created by a call to EnableAsyncSend() allowing up to
 /// the number of milliseconds in timeout to shutdown the worker and complete any 
 /// pending work.  If timeout is 0, the worker will be aborted.
 /// </summary>
 public void StopAsyncSending(bool completeWork, int timeout)
 {
     WaitAndContinueWorker kill;
     lock (_sync)
     {
         kill = _deferred;
         _deferred = null;
     }
     if (kill != null)
     {
         using (kill)
             kill.Complete(completeWork, timeout);
     }
 }
        public void TestAddWorkListToDisposedWorker()
        {
            WaitAndContinueWorker work = new WaitAndContinueWorker();
            work.Complete(false, 100);
            work.Dispose();

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

            work.AddWork(list);
        }
 /// <summary>
 /// Enables a background worker thread to continue sending messages that are incomplete 
 /// after the expiration of the timeout specified in the Broadcast/SendTo method.  This
 /// is required to avoid dead-locks if your broadcasting messages within an IpcEvent.OnEvent
 /// event handler.
 /// </summary>
 public void EnableAsyncSend()
 {
     lock (_sync)
         _deferred = _deferred ?? new WaitAndContinueWorker();
 }
        public void TestAddWorkToDisposedWorker()
        {
            WaitAndContinueWorker work = new WaitAndContinueWorker();
            work.Complete(false, 100);
            work.Dispose();

            SampleWork item = new SampleWork();
            work.AddWork(item);
        }
        public void TestWorkerException()
        {
            using (WaitAndContinueWorker work = new WaitAndContinueWorker())
            {
                SampleWork item = new SampleWork();
                item.WorkThrows = true;
                work.AddWork(item);

                ManualResetEvent mreError = new ManualResetEvent(false);
                Exception error = null;
                work.OnError += delegate(object o, ErrorEventArgs e) { error = e.GetException(); mreError.Set(); };

                item.Ready.Set();
                Assert.IsTrue(mreError.WaitOne(1000, false));
                Assert.IsTrue(error is ArgumentOutOfRangeException);
                Assert.IsTrue(((ArgumentOutOfRangeException)error).ParamName == "WorkThrows");
            }
        }
        public void TestHardAbort()
        {
            using (WaitAndContinueWorker work = new WaitAndContinueWorker())
            {
                work.OnError += delegate(object o, ErrorEventArgs e) { throw new Exception("Failed.", e.GetException()); };

                SampleWork item = new SampleWork();
                item.SleepForever = true;
                work.AddWork(item);

                item.Ready.Set();
                Assert.IsTrue(item.Sleeping.WaitOne(1000, false));
                work.Abort();

                Assert.IsFalse(item.Completed);
                Assert.IsTrue(item.Disposed);
            }
        }
        public void TestAbortWork()
        {
            using (WaitAndContinueWorker work = new WaitAndContinueWorker())
            {
                work.OnError += delegate(object o, ErrorEventArgs e) { throw new Exception("Failed.", e.GetException()); };

                SampleWork item = new SampleWork();
                work.AddWork(item);

                Assert.IsFalse(work.IsEmpty);
                work.Abort();

                Assert.IsFalse(item.Completed);
                Assert.IsTrue(item.Disposed);
            }
        }
        public void TestWaitAndContinueWorker()
        {
            WaitAndContinueWorker work;
            using (work = new WaitAndContinueWorker())
            {
                work.OnError += delegate(object o, ErrorEventArgs e) { throw new Exception("Failed.", e.GetException()); };

                SampleWork item = new SampleWork();
                work.AddWork(item);

                Assert.IsFalse(work.IsEmpty);

                item.Ready.Set();
                Assert.IsTrue(item.CompletedEvent.WaitOne(100, false));
                Assert.IsTrue(item.Completed);
                Assert.IsFalse(item.Cancelled);

                while (!item.Disposed)
                    Thread.Sleep(0);

                Assert.IsTrue(item.Disposed);
                Assert.IsTrue(work.IsEmpty);
            }
            Assert.IsTrue(work.IsEmpty);
        }