public void DoWork() 
		{ 
			SmartThreadPool smartThreadPool = new SmartThreadPool();

			DivArgs divArgs = new DivArgs();
			divArgs.x = 10;
			divArgs.y = 0;

			IWorkItemResult wir = 
				smartThreadPool.QueueWorkItem(new 
					WorkItemCallback(this.DoDiv), divArgs);

			Exception e;
			object obj = wir.GetResult(out e);
			// e contains the expetion that DoDiv threw
			if(null == e)
			{
				int result = (int)obj;
			}
			else
			{
				// Do something with the exception
			}

			smartThreadPool.Shutdown();
		} 
        private static int calculateLabel(Individual[] weights)
        {
            Individual[] reversedWeights = { weights[1], weights[0] };
            weights[1].ResetScores();
            weights[0].ResetScores();
            SmartThreadPool smtp = new SmartThreadPool();
            for (int index = 0; index < numberOfGames; index++)
            {

                if (index % 2 == 1)
                {
                    smtp.QueueWorkItem(new WorkItemCallback(threadProc), weights);
                }
                else
                {
                    smtp.QueueWorkItem(new WorkItemCallback(threadProc), reversedWeights);
                }

            }
            smtp.WaitForIdle();
            smtp.Shutdown();
            //double strengthRatio = ((double)weights[0].Wins + (double)weights[0].Draws / 2) / numberOfGames;
            //return strengthRatio > 0.5 ? 1 : -1;
            return weights[0].Wins + 1/2 * weights[0].Draws;
        }
示例#3
0
        public void TestJoin()
        {
            SmartThreadPool stp = new SmartThreadPool();

            SafeCounter sc = new SafeCounter();

            stp.Join(
                sc.Increment,
                sc.Increment,
                sc.Increment);

            Assert.AreEqual(3, sc.Counter);

            for (int j = 0; j < 10; j++)
            {
                sc.Reset();

                Action[] actions = new Action[1000];
                for (int i = 0; i < actions.Length; i++)
                {
                    actions[i] = sc.Increment;
                }

                stp.Join(actions);

                Assert.AreEqual(actions.Length, sc.Counter);
            }

            stp.Shutdown();
        }
        public void TestMaxThreadsChange()
        {
            SmartThreadPool smartThreadPool = new SmartThreadPool(1 * 1000, 1, 0);

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

            bool success = WaitForMaxThreadsValue(smartThreadPool, 1, 1 * 1000);
            Assert.IsTrue(success);

            smartThreadPool.MaxThreads = 5;
            success = WaitForMaxThreadsValue(smartThreadPool, 5, 2 * 1000);
            Assert.IsTrue(success);

            smartThreadPool.MaxThreads = 25;
            success = WaitForMaxThreadsValue(smartThreadPool, 25, 4 * 1000);
            Assert.IsTrue(success);

            smartThreadPool.MaxThreads = 10;
            success = WaitForMaxThreadsValue(smartThreadPool, 10, 10 * 1000);
            Assert.IsTrue(success);

            smartThreadPool.Shutdown();
        }
        public void TestWIGConcurrencyChange()
        {
            SmartThreadPool smartThreadPool = new SmartThreadPool(10 * 1000, 25, 0);

            IWorkItemsGroup wig = smartThreadPool.CreateWorkItemsGroup(smartThreadPool.MaxThreads);
            bool success = false;

            for (int i = 0; i < 100; ++i)
            {
                wig.QueueWorkItem(new WorkItemCallback(this.DoSomeLongWork), null);
            }

            wig.Concurrency = 1;
            success = WaitForWIGThreadsInUse(wig, 1, 1 * 1000);
            Assert.IsTrue(success);

            wig.Concurrency = 5;
            success = WaitForWIGThreadsInUse(wig, 5, 2 * 1000);
            Assert.IsTrue(success);

            wig.Concurrency = 25;
            success = WaitForWIGThreadsInUse(wig, 25, 4 * 1000);
            Assert.IsTrue(success);

            wig.Concurrency = 10;
            success = WaitForWIGThreadsInUse(wig, 10, 10 * 1000);
            Assert.IsTrue(success);

            smartThreadPool.Shutdown();
        }
		public void WaitForIdleOnSTPThreadForAnotherWorkItemsGroup()
		{ 
			SmartThreadPool smartThreadPool = new SmartThreadPool(10*1000, 25, 0);
			IWorkItemsGroup workItemsGroup1 = smartThreadPool.CreateWorkItemsGroup(int.MaxValue);
			IWorkItemsGroup workItemsGroup2 = smartThreadPool.CreateWorkItemsGroup(int.MaxValue);

			workItemsGroup1.QueueWorkItem(
				new WorkItemCallback(this.DoSomeWork), 
				1000);

			workItemsGroup1.QueueWorkItem(
				new WorkItemCallback(this.DoSomeWork), 
				1000);

			IWorkItemResult wir = workItemsGroup2.QueueWorkItem(
				new WorkItemCallback(this.DoWaitForIdle), 
				workItemsGroup1);

			Exception e;
			wir.GetResult(out e);

			smartThreadPool.Shutdown();

			Assert.IsNull(e);
		} 
        public void GoodCallback()
        {
            SmartThreadPool stp = new SmartThreadPool();

            stp.QueueWorkItem(new WorkItemCallback(DoWork));

            stp.WaitForIdle();

            stp.Shutdown();
        }
        public void GoodCallback()
        {
            SmartThreadPool smartThreadPool = new SmartThreadPool();
            IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue);

            workItemsGroup.QueueWorkItem(new WorkItemCallback(DoWork));

            workItemsGroup.WaitForIdle();

            smartThreadPool.Shutdown();
        }
        public void ChainedDelegatesCallback()
        {
            SmartThreadPool stp = new SmartThreadPool();

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

            stp.QueueWorkItem(workItemCallback);

            stp.WaitForIdle();

            stp.Shutdown();
        }
        public void GoodPostExecute()
        {
            SmartThreadPool stp = new SmartThreadPool();

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

            stp.WaitForIdle();

            stp.Shutdown();
        }
        public void DoWork(int[] numbers)
        {
            SmartThreadPool smartThreadPool = new SmartThreadPool();

            // Queue the work item
            IWorkItemResult<double> wir = smartThreadPool.QueueWorkItem(new Func<int[], double>(CalcAverage), numbers);

            // Do some other work here

            // Get the result of the operation
            double average = wir.Result;

            smartThreadPool.Shutdown();
        }
        public void ChainedDelegatesCallback()
        {
            SmartThreadPool smartThreadPool = new SmartThreadPool();
            IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue);

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

            workItemsGroup.QueueWorkItem(workItemCallback);

            workItemsGroup.WaitForIdle();

            smartThreadPool.Shutdown();
        }
        public void WaitForIdleOnWrongThread()
        {
            SmartThreadPool smartThreadPool = new SmartThreadPool(10*1000, 25, 0);

            IWorkItemResult wir = smartThreadPool.QueueWorkItem(
                new WorkItemCallback(this.DoWaitForIdle),
                smartThreadPool);

            Exception e;
            wir.GetResult(out e);

            smartThreadPool.Shutdown();

            Assert.IsTrue(e is NotSupportedException);
        }
		public void DoWork(object [] states) 
		{ 
			SmartThreadPool smartThreadPool = new SmartThreadPool();

			foreach(object state in states)
			{
				smartThreadPool.QueueWorkItem(new 
					WorkItemCallback(this.DoSomeWork), state);
			}

			// Wait for the completion of all work items
			smartThreadPool.WaitForIdle();

			smartThreadPool.Shutdown();
		} 
        public void WaitForIdleEvent()
        {
            SmartThreadPool smartThreadPool = new SmartThreadPool();
            IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(1);
            ManualResetEvent wigIsIdle = new ManualResetEvent(false);

            workItemsGroup.OnIdle += wig => wigIsIdle.Set();

            workItemsGroup.QueueWorkItem(() => { });

            bool eventFired = wigIsIdle.WaitOne(100, true);

            smartThreadPool.Shutdown();

            Assert.IsTrue(eventFired);
        }
        public void ChainedDelegatesPostExecute()
        {
            SmartThreadPool stp = new SmartThreadPool();

            PostExecuteWorkItemCallback postExecuteWorkItemCallback = DoPostExecute;
            postExecuteWorkItemCallback += DoPostExecute;

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

            stp.WaitForIdle();

            stp.Shutdown();
        }
        public void Timeout() 
		{ 
			SmartThreadPool smartThreadPool = new SmartThreadPool();

			IWorkItemResult wir = 
				smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);

            try
            {
                wir.GetResult(500, true);
            }
            finally
            {
                smartThreadPool.Shutdown();
            }
		}
        public void ChainedDelegatesCallback()
        {
            Assert.Throws<NotSupportedException>(() =>
            {

                SmartThreadPool stp = new SmartThreadPool();

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

                stp.QueueWorkItem(workItemCallback);

                stp.WaitForIdle();

                stp.Shutdown();
            });
        }
		public void DoWork(object [] states) 
		{ 
			SmartThreadPool smartThreadPool = new SmartThreadPool();

			IWorkItemsGroup wig = smartThreadPool.CreateWorkItemsGroup(1);

            wig.OnIdle += wig_OnIdle;

			foreach(object state in states)
			{
				wig.QueueWorkItem(new 
					WorkItemCallback(this.DoSomeWork), state);
			}

			smartThreadPool.WaitForIdle();
			smartThreadPool.Shutdown();
		} 
        public void TestThreadsEvents()
        {
            ClearResults();

            SmartThreadPool stp = new SmartThreadPool();

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

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

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

            Assert.IsTrue(_initSuccess);
            Assert.IsTrue(_workItemSuccess);
            Assert.IsTrue(_termSuccess);
        }
        public void DoWork(object state)
        {
            SmartThreadPool smartThreadPool = new SmartThreadPool();

            // Queue the work item
            IWorkItemResult wir = smartThreadPool.QueueWorkItem(DoRealWork);

            // Give the work item some time to complete.
            Thread.Sleep(1000);

            // If the work item hasn't completed yet then cancel it.
            if (!wir.IsCompleted)
            {
                wir.Cancel();
            }

            smartThreadPool.Shutdown();
        }
        public void ChainedDelegatesCallback()
        {
            Assert.Throws<NotSupportedException>(() =>
            {

                SmartThreadPool smartThreadPool = new SmartThreadPool();
                IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue);

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

                workItemsGroup.QueueWorkItem(workItemCallback);

                workItemsGroup.WaitForIdle();

                smartThreadPool.Shutdown();
            });
        }
        public void TimeoutCompletedWorkItem()
        {
            SmartThreadPool stp = new SmartThreadPool();
            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();
        }
        public void ChainedDelegatesPostExecute()
        {
            SmartThreadPool smartThreadPool = new SmartThreadPool();
            IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue);

            PostExecuteWorkItemCallback postExecuteWorkItemCallback =
                new PostExecuteWorkItemCallback(DoPostExecute);
            postExecuteWorkItemCallback +=
                new PostExecuteWorkItemCallback(DoPostExecute);

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

            workItemsGroup.WaitForIdle();

            smartThreadPool.Shutdown();
        }
		private void Concurrency(
			int concurrencyPerWig,
			int wigsCount,
			int workItemsCount)
		{
			Console.WriteLine(
				"Testing : concurrencyPerWig = {0}, wigsCount = {1}, workItemsCount = {2}",
				concurrencyPerWig,
				wigsCount,
				workItemsCount);

			_success = true;
			_concurrencyPerWig = concurrencyPerWig;
			_randGen = new Random(0);

			STPStartInfo stpStartInfo = new STPStartInfo();
			stpStartInfo.StartSuspended = true;

			SmartThreadPool stp = new SmartThreadPool(stpStartInfo);

			_concurrentOps = new int[wigsCount];

			IWorkItemsGroup [] wigs = new IWorkItemsGroup[wigsCount];

			for(int i = 0; i < wigs.Length; ++i)
			{
				wigs[i] = stp.CreateWorkItemsGroup(_concurrencyPerWig);
				for(int j = 0; j < workItemsCount; ++j)
				{
					wigs[i].QueueWorkItem(new WorkItemCallback(this.DoWork), i);
				}

				wigs[i].Start();
			}

			stp.Start();

			stp.WaitForIdle();

			Assert.IsTrue(_success);

			stp.Shutdown();
		}
        public void DoWork()
        {
            SmartThreadPool smartThreadPool = new SmartThreadPool();

            IWorkItemResult<double> wir = smartThreadPool.QueueWorkItem(new Func<double, double, double>(DoDiv), 10.0, 0.0);

            try
            {
                double result = wir.Result;
            }
            // Catch the exception that Result threw
            catch (WorkItemResultException e)
            {
                // Dump the inner exception which DoDiv threw
                Debug.WriteLine(e.InnerException);
            }

            smartThreadPool.Shutdown();
        }
        public void DoWork(object [] states)
        {
            SmartThreadPool smartThreadPool = new SmartThreadPool();

            // Create a work items group that processes
            // one work item at a time
            IWorkItemsGroup wig = smartThreadPool.CreateWorkItemsGroup(1);

            // Queue some work items
            foreach(object state in states)
            {
                wig.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), state);
            }

            // Wait for the completion of all work items in the work items group
            wig.WaitForIdle();

            smartThreadPool.Shutdown();
        }
示例#28
0
        public void FuncT()
        {
            SmartThreadPool stp = new SmartThreadPool();
            IWorkItemResult<int> wir =
                stp.QueueWorkItem(new Func<int, int>(f), 1);

            int y = wir.GetResult();

            Assert.AreEqual(y, 2);

            try
            {
                wir.GetResult();
            }
            finally
            {
                stp.Shutdown();
            }
        }
		public void BlockingCall() 
		{ 
			SmartThreadPool smartThreadPool = new SmartThreadPool();

			bool success = false;

			IWorkItemResult wir = 
				smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);

			if (!wir.IsCompleted)
			{
				int result = (int)wir.GetResult();
				success = (1 == result);
			}

			smartThreadPool.Shutdown();

			Assert.IsTrue(success);
		} 
示例#30
0
        public void TestChoice()
        {
            SmartThreadPool stp = new SmartThreadPool();

            int index = stp.Choice(
                () => Thread.Sleep(1000),
                () => Thread.Sleep(1500),
                () => Thread.Sleep(500));

            Assert.AreEqual(2, index);

            index = stp.Choice(
                () => Thread.Sleep(300),
                () => Thread.Sleep(100),
                () => Thread.Sleep(200));

            Assert.AreEqual(1, index);

            stp.Shutdown();
        }