示例#1
0
        public void CancelInProgressWorkItemSoftWithIgnoreSample()
        {
            Assert.ThrowsException <WorkItemCancelException>(() =>
            {
                ManualResetEvent waitToStart    = new ManualResetEvent(false);
                ManualResetEvent waitToComplete = new ManualResetEvent(false);

                STP stp             = new STP();
                IWorkItemResult wir = stp.QueueWorkItem(
                    state =>
                {
                    waitToStart.Set();
                    Thread.Sleep(100);
                    waitToComplete.WaitOne();
                    return(null);
                }
                    );

                waitToStart.WaitOne();

                wir.Cancel(false);

                waitToComplete.Set();

                stp.WaitForIdle();

                // Throws WorkItemCancelException
                wir.GetResult();

                stp.Shutdown();
            });
        }
示例#2
0
        public void CancelInProgressWorkItemSoftWithAbortOnWorkItemCancel()
        {
            bool             abortFailed  = false;
            ManualResetEvent waitToStart  = new ManualResetEvent(false);
            ManualResetEvent waitToCancel = new ManualResetEvent(false);

            STP             stp = new STP();
            IWorkItemResult wir = stp.QueueWorkItem(
                state =>
            {
                waitToStart.Set();
                waitToCancel.WaitOne();
                STP.AbortOnWorkItemCancel();
                abortFailed = true;
                return(null);
            });

            waitToStart.WaitOne();

            wir.Cancel(false);

            waitToCancel.Set();

            stp.WaitForIdle();

            Assert.IsTrue(wir.IsCanceled);
            Assert.IsFalse(abortFailed);

            stp.Shutdown();
        }
        private object DoWaitForIdle(object state)
        {
            STP smartThreadPool = state as STP;

            smartThreadPool.WaitForIdle();
            return(null);
        }
示例#4
0
        public void CancelInProgressWorkItemSoftWithSample()
        {
            bool             cancelled      = false;
            ManualResetEvent waitToStart    = new ManualResetEvent(false);
            ManualResetEvent waitToComplete = new ManualResetEvent(false);

            STP             stp = new STP();
            IWorkItemResult wir = stp.QueueWorkItem(
                state =>
            {
                waitToStart.Set();
                waitToComplete.WaitOne();
                cancelled = STP.IsWorkItemCanceled;
                return(null);
            }
                );

            waitToStart.WaitOne();

            wir.Cancel(false);

            waitToComplete.Set();

            stp.WaitForIdle();

            Assert.IsTrue(cancelled);

            stp.Shutdown();
        }
        public void TimeoutInProgressWorkItemSoftWithAbortOnWorkItemCancel()
        {
            bool             abortFailed    = false;
            ManualResetEvent waitToStart    = new ManualResetEvent(false);
            ManualResetEvent waitToComplete = new ManualResetEvent(false);

            STP             stp = new STP();
            IWorkItemResult wir = stp.QueueWorkItem(
                new WorkItemInfo()
            {
                Timeout = 500
            },
                state =>
            {
                waitToStart.Set();
                Thread.Sleep(1000);
                STP.AbortOnWorkItemCancel();
                abortFailed = true;
                return(null);
            });

            waitToStart.WaitOne();

            stp.WaitForIdle();

            Assert.IsTrue(wir.IsCanceled);
            Assert.IsFalse(abortFailed);
            stp.Shutdown();
        }
示例#6
0
        public void DisposeCallerState()
        {
            STPStartInfo stpStartInfo = new STPStartInfo
            {
                DisposeOfStateObjects = true
            };

            STP smartThreadPool = new STP(stpStartInfo);

            CallerState nonDisposableCallerState = new NonDisposableCallerState();
            CallerState disposableCallerState    = new DisposableCallerState();

            IWorkItemResult wir1 =
                smartThreadPool.QueueWorkItem(
                    new WorkItemCallback(this.DoSomeWork),
                    nonDisposableCallerState);

            IWorkItemResult wir2 =
                smartThreadPool.QueueWorkItem(
                    new WorkItemCallback(this.DoSomeWork),
                    disposableCallerState);

            wir1.GetResult();
            Assert.AreEqual(1, nonDisposableCallerState.Value);

            wir2.GetResult();

            // Wait a little bit for the working thread to call dispose on the
            // work item's state.
            smartThreadPool.WaitForIdle();

            Assert.AreEqual(2, disposableCallerState.Value);

            smartThreadPool.Shutdown();
        }
示例#7
0
        public void GoodCallback()
        {
            STP stp = new STP();

            stp.QueueWorkItem(new WorkItemCallback(DoWork));

            stp.WaitForIdle();

            stp.Shutdown();
        }
        public void WaitForIdle()
        {
            STP smartThreadPool = new STP(10 * 1000, 25, 0);

            ManualResetEvent isRunning = new ManualResetEvent(false);

            for (int i = 0; i < 4; ++i)
            {
                smartThreadPool.QueueWorkItem(delegate { isRunning.WaitOne(); });
            }

            bool success = !smartThreadPool.WaitForIdle(1000);

            isRunning.Set();

            success = success && smartThreadPool.WaitForIdle(1000);

            smartThreadPool.Shutdown();

            Assert.IsTrue(success);
        }
        public void ExceptionType()
        {
            STP smartThreadPool = new STP();

            var workItemResult = smartThreadPool.QueueWorkItem(new Func <int>(ExceptionMethod));

            smartThreadPool.WaitForIdle();

            Assert.IsInstanceOfType(workItemResult.Exception, typeof(NotImplementedException));

            smartThreadPool.Shutdown();
        }
示例#10
0
        public void GoodPostExecute()
        {
            STP stp = new STP();

            stp.QueueWorkItem(
                new WorkItemCallback(DoWork),
                null,
                new PostExecuteWorkItemCallback(DoPostExecute));

            stp.WaitForIdle();

            stp.Shutdown();
        }
示例#11
0
        public void ChainedDelegatesCallback()
        {
            Assert.ThrowsException <NotSupportedException>(() =>
            {
                STP stp = new STP();

                WorkItemCallback workItemCallback = new WorkItemCallback(DoWork);
                workItemCallback += new WorkItemCallback(DoWork);

                stp.QueueWorkItem(workItemCallback);

                stp.WaitForIdle();

                stp.Shutdown();
            });
        }
示例#12
0
        private static void CheckApartmentState(ApartmentState requestApartmentState)
        {
            STPStartInfo stpStartInfo = new STPStartInfo
            {
                ApartmentState = requestApartmentState
            };

            STP stp = new STP(stpStartInfo);

            IWorkItemResult <ApartmentState> wir = stp.QueueWorkItem(() => GetCurrentThreadApartmentState());

            ApartmentState resultApartmentState = wir.GetResult();

            stp.WaitForIdle();

            Assert.AreEqual(requestApartmentState, resultApartmentState);
        }
示例#13
0
        public void CancelCompletedWorkItem()
        {
            STP             stp = new STP();
            IWorkItemResult wir = stp.QueueWorkItem(
                state => 1
                );

            stp.WaitForIdle();

            Assert.AreEqual(wir.GetResult(), 1);

            wir.Cancel();

            Assert.AreEqual(wir.GetResult(), 1);

            stp.Shutdown();
        }
示例#14
0
        //[ExpectedException(typeof(WorkItemCancelException))]
        public void CancelCancelledWorkItemAbort()
        {
            Assert.ThrowsException <WorkItemCancelException>(() =>
            {
                ManualResetEvent waitToStart = new ManualResetEvent(false);

                STP stp             = new STP();
                IWorkItemResult wir = stp.QueueWorkItem(
                    state =>
                {
                    waitToStart.Set();
                    while (true)
                    {
                        Thread.Sleep(1000);
                    }
#pragma warning disable CS0162 // Unreachable code detected
                    return(null);

#pragma warning restore CS0162 // Unreachable code detected
                }
                    );

                waitToStart.WaitOne();

                wir.Cancel(false);

                Assert.IsTrue(wir.IsCanceled);

                bool completed = stp.WaitForIdle(1000);

                Assert.IsFalse(completed);

                wir.Cancel(true);

                try
                {
                    wir.GetResult();
                }
                finally
                {
                    stp.Shutdown();
                }
            });
        }
示例#15
0
        public void TestThreadsEvents()
        {
            ClearResults();

            STP stp = new STP();

            stp.OnThreadInitialization += OnInitialization;
            stp.OnThreadTermination    += OnTermination;

            stp.QueueWorkItem(new WorkItemCallback(DoSomeWork), null);

            stp.WaitForIdle();
            stp.Shutdown();

            Thread.Sleep(500); // Wait for the STP to shutdown.
            Assert.IsTrue(_initSuccess);
            Assert.IsTrue(_workItemSuccess);
            Assert.IsTrue(_termSuccess);
        }
示例#16
0
        public void ChainedDelegatesPostExecute()
        {
            Assert.ThrowsException <NotSupportedException>(() =>
            {
                STP stp = new STP();

                PostExecuteWorkItemCallback postExecuteWorkItemCallback = DoPostExecute;
                postExecuteWorkItemCallback += DoPostExecute;

                stp.QueueWorkItem(
                    new WorkItemCallback(DoWork),
                    null,
                    postExecuteWorkItemCallback);

                stp.WaitForIdle();

                stp.Shutdown();
            });
        }
        public void CancelInQueueWorkItem()
        {
            Assert.ThrowsException <WorkItemCancelException>(() =>
            {
                STPStartInfo stpStartInfo   = new STPStartInfo();
                stpStartInfo.StartSuspended = true;

                bool hasRun = false;

                STP stp             = new STP(stpStartInfo);
                IWorkItemResult wir = stp.QueueWorkItem(
                    new WorkItemInfo()
                {
                    Timeout = 500
                },
                    state =>
                {
                    hasRun = true;
                    return(null);
                });

                Assert.IsFalse(wir.IsCanceled);

                Thread.Sleep(2000);

                Assert.IsTrue(wir.IsCanceled);

                stp.Start();
                stp.WaitForIdle();

                Assert.IsFalse(hasRun);

                try
                {
                    wir.GetResult();
                }
                finally
                {
                    stp.Shutdown();
                }
            });
        }
        public void TimeoutCompletedWorkItem()
        {
            STP             stp = new STP();
            IWorkItemResult wir =
                stp.QueueWorkItem(
                    new WorkItemInfo()
            {
                Timeout = 500
            },
                    state => 1);

            stp.WaitForIdle();

            Assert.AreEqual(wir.GetResult(), 1);

            Thread.Sleep(1000);

            Assert.AreEqual(wir.GetResult(), 1);

            stp.Shutdown();
        }
示例#19
0
        public void Cancel1WIGof2WorkItems()
        {
            int counter1 = 0;
            int counter2 = 0;

            STP             stp  = new STP();
            IWorkItemsGroup wig1 = stp.CreateWorkItemsGroup(3);
            IWorkItemsGroup wig2 = stp.CreateWorkItemsGroup(3);

            for (int i = 0; i < 3; i++)
            {
                wig1.QueueWorkItem(
                    state => { Interlocked.Increment(ref counter1); Thread.Sleep(500); Interlocked.Increment(ref counter1); return(null); }
                    );
            }

            for (int i = 0; i < 3; i++)
            {
                wig2.QueueWorkItem(
                    state => { Thread.Sleep(500); Interlocked.Increment(ref counter2); return(null); }
                    );
            }

            while (counter1 < 3)
            {
                Thread.Sleep(1);
            }
            wig1.Cancel(true);

            stp.WaitForIdle();

            Assert.AreEqual(3, counter1, "Cancelled WIG1");
            Assert.AreEqual(3, counter2, "Normal WIG2");

            stp.Shutdown();
        }