Beispiel #1
0
        public void WaitAny()
        {
            SmartThreadPool smartThreadPool = new SmartThreadPool();
            IWorkItemsGroup workItemsGroup  = smartThreadPool.CreateWorkItemsGroup(int.MaxValue);

            bool success = false;

            IWorkItemResult [] wirs = new IWorkItemResult[5];

            for (int i = 0; i < wirs.Length; ++i)
            {
                wirs[i] =
                    workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);
            }

            int index = SmartThreadPool.WaitAny(wirs);

            if (wirs[index].IsCompleted)
            {
                int result = (int)wirs[index].GetResult();
                if (1 == result)
                {
                    success = true;
                }
            }

            smartThreadPool.Shutdown();

            Assert.IsTrue(success);
        }
Beispiel #2
0
        public void WaitAnyT()
        {
            SmartThreadPool smartThreadPool = new SmartThreadPool();

            bool success = false;

            IWorkItemResult <int>[] wirs = new IWorkItemResult <int> [5];

            for (int i = 0; i < wirs.Length; ++i)
            {
                wirs[i] = smartThreadPool.QueueWorkItem(new Func <int, int, int>(System.Math.Max), i, i - 1);
            }

            int index = SmartThreadPool.WaitAny(wirs);

            if (wirs[index].IsCompleted)
            {
                int result = wirs[index].GetResult();
                if (index == result)
                {
                    success = true;
                }
            }

            smartThreadPool.Shutdown();

            Assert.IsTrue(success);
        }
Beispiel #3
0
        public void WaitAnyWithTimeoutFailure()
        {
            SmartThreadPool smartThreadPool = new SmartThreadPool();
            IWorkItemsGroup workItemsGroup  = smartThreadPool.CreateWorkItemsGroup(int.MaxValue);

            IWorkItemResult [] wirs = new IWorkItemResult[5];

            for (int i = 0; i < wirs.Length; ++i)
            {
                wirs[i] =
                    workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);
            }

            int index = SmartThreadPool.WaitAny(wirs, 10, true);

            bool success = (index == WaitHandle.WaitTimeout);

            smartThreadPool.Shutdown();

            Assert.IsTrue(success);
        }
Beispiel #4
0
        public void WaitAnyWithTimeoutSuccess()
        {
            SmartThreadPool smartThreadPool = new SmartThreadPool();

            bool success = true;

            IWorkItemResult [] wirs = new IWorkItemResult[5];

            for (int i = 0; i < wirs.Length; ++i)
            {
                wirs[i] =
                    smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);
            }

            int index = SmartThreadPool.WaitAny(wirs, 1500, true);

            success = (index != WaitHandle.WaitTimeout);

            smartThreadPool.Shutdown();

            Assert.IsTrue(success);
        }
        public void DoWork()
        {
            SmartThreadPool smartThreadPool = new SmartThreadPool();

            IWorkItemResult wir1 =
                smartThreadPool.QueueWorkItem(new
                                              WorkItemCallback(this.DoSomeWork1), null);

            IWorkItemResult wir2 =
                smartThreadPool.QueueWorkItem(new
                                              WorkItemCallback(this.DoSomeWork2), null);

            IWorkItemResult [] wirs = new IWorkItemResult [] { wir1, wir2 };

            int index = SmartThreadPool.WaitAny(wirs);

            if (index != WaitHandle.WaitTimeout)
            {
                int result = (int)wirs[index].Result;
            }

            smartThreadPool.Shutdown();
        }