Smart thread pool class.
Inheritance: IWorkItemsGroup, IDisposable
示例#1
0
        public void WorkItemWaitCanceling()
        {
            Assert.Throws<WorkItemTimeoutException>(() =>
            {
                SmartThreadPool smartThreadPool = new SmartThreadPool();

                ManualResetEvent cancelWaitHandle = new ManualResetEvent(false);

                // Queue a work item that will occupy the thread in the pool
                IWorkItemResult wir1 =
                    smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);

                // Queue another work item that will wait for the first to complete
                IWorkItemResult wir2 =
                    smartThreadPool.QueueWorkItem(new WorkItemCallback(this.SignalCancel), cancelWaitHandle);

                try
                {
                    wir1.GetResult(System.Threading.Timeout.Infinite, true, cancelWaitHandle);
                }
                finally
                {
                    smartThreadPool.Shutdown();
                }
            });
        }
		public void WorkItemWaitCanceling() 
		{ 
			SmartThreadPool smartThreadPool = new SmartThreadPool();
			IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue);

			ManualResetEvent cancelWaitHandle = new ManualResetEvent(false);

			bool success = false;

			// Queue a work item that will occupy the thread in the pool
			IWorkItemResult wir1 = 
				workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);

			// Queue another work item that will wait for the first to complete
			IWorkItemResult wir2 = 
				workItemsGroup.QueueWorkItem(new WorkItemCallback(this.SignalCancel), cancelWaitHandle);

			try
			{
				wir1.GetResult(System.Threading.Timeout.Infinite, true, cancelWaitHandle);
			}
			catch (WorkItemTimeoutException) 
			{
				success = true;
			}

			smartThreadPool.Shutdown();

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

            bool success = true;

            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.Min), i, i + 1);
            }

            SmartThreadPool.WaitAll(wirs);

            for (int i = 0; i < wirs.Length; ++i)
            {
                if (!wirs[i].IsCompleted)
                {
                    success = false;
                    break;
                }

                int result = wirs[i].GetResult();
                if (i != result)
                {
                    success = false;
                    break;
                }
            }

            smartThreadPool.Shutdown();

            Assert.IsTrue(success);
        }
        public static void getImagesAsync(List<FilmData> tempFilmList)
        {
            if (isNetworkAvailable())
            {

                SmartThreadPool smartThreadPool = new SmartThreadPool(
                                                                        15 * 1000,   // Idle timeout in milliseconds
                                                                        25,         // Threads upper limit
                                                                        1);         // Threads lower limit
                var bw = new System.ComponentModel.BackgroundWorker();
                bw.DoWork += (s, args) =>
                {
                    foreach (FilmData item in tempFilmList)
                    {
                        if (IsolatedStorageHelper.isFileAlreadySaved(item.imageUrl))
                            ImageHelper.addDownloadedItemsToFilmCollection(item.imageUrl);
                        else
                            smartThreadPool.QueueWorkItem(() => getImageFromUrl(item.imageUrl));
                    }

                    smartThreadPool.WaitForIdle(); // Wait for the completion of all work items
                };
                bw.RunWorkerAsync();
            }
        }
        public void CancelCanceledWorkItem()
        {
            STPStartInfo stpStartInfo = new STPStartInfo();
            stpStartInfo.StartSuspended = true;

            SmartThreadPool stp = new SmartThreadPool(stpStartInfo);
            IWorkItemResult wir = stp.QueueWorkItem(state => null);

            int counter = 0;

            wir.Cancel();

            try
            {
                wir.GetResult();
            }
            catch (WorkItemCancelException ce)
            {
                ce.GetHashCode();
                ++counter;
            }

            Assert.AreEqual(counter, 1);

            wir.Cancel();

            try
            {
                wir.GetResult();
            }
            finally
            {
                stp.Shutdown();
            }
        }
示例#6
0
        public void test()
        {
            SmartThreadPool smartThreadPool = new SmartThreadPool();

            Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
            GlobalVar.isCrawling = true;
            //string conn = string.Format(@"Data Source="+System.Environment.CurrentDirectory+"\\data\\{0}", GlobalVar.filename);
            sqlitehelper con = new sqlitehelper(System.Environment.CurrentDirectory + @"\data\" + GlobalVar.filename);
            string sql = "select keyword from Content where flag==0";
            DataTable dt = new DataTable();
            dt = con.GetDataTable(sql);
            IWorkItemResult[] wir = new IWorkItemResult[dt.Rows.Count];
            MessageBox.Show(dt.Rows.Count.ToString());
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //MessageBox.Show(dt.Rows[i]["keyword"].ToString());
                OneWorker one = new OneWorker(dt.Rows[i]["keyword"].ToString());
                ThreadPool.QueueUserWorkItem(one.work, i);

            }

            bool success = SmartThreadPool.WaitAll(
                   wir);

            if (success)
            {
                GlobalVar.isCrawling = false;

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

            bool success = false;

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

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

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

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

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

            smartThreadPool.Shutdown();
        }
示例#8
0
        public void Test()
        {
            _requestReceived = new ManualResetEvent(false);

            /*setup + start listening*/
            var stub = new SipReceivedMessageProcessorStub(OnRequestReceived, (s,e) => { });

            var listeningPoint = new IPEndPoint(TestConstants.MyIpAddress, 33333);

            var f = new SipFactory();

            var stp = new SmartThreadPool();
            stp.Start();
            var provider = new SipContextSource(listeningPoint, stp);
            provider.AddListener(null);
            provider.Start();

            /*send a message to the listener*/
            var send = new SipRequestBuilder().Build();
            var requestBytes = SipFormatter.FormatMessage(send);

            var udpClient = new UdpClient();
            udpClient.Send(requestBytes, requestBytes.Length, listeningPoint);

            _requestReceived.WaitOne();

            var oc = ObjectComparer.Create();

            var received = stub.Requests.First();
            oc.Compare(received, send);
            Assert.True(oc.Differences.Count == 0, oc.DifferencesString);
        }
		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();
		} 
        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();
        }
示例#11
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();
        }
示例#12
0
        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;
        }
		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);
		} 
        public void ExceptionThrowing()
        {
            SmartThreadPool _smartThreadPool = new SmartThreadPool();

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

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

            try
            {
                wir.GetResult();
            }
            catch(WorkItemResultException wire)
            {
                Assert.IsTrue(wire.InnerException is DivideByZeroException);
                return;
            }
            catch(Exception e)
            {
                e.GetHashCode();
                Assert.Fail();
            }
            Assert.Fail();
        }
        public void ExceptionReturning()
        {
            bool success = true;

            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 = null;
            try
            {
                wir.GetResult(out e);
            }
            catch (Exception ex)
            {
                ex.GetHashCode();
                success = false;
            }

            Assert.IsTrue(success);
            Assert.IsTrue(e is DivideByZeroException);
        }
        public void TimeoutInProgressWorkItemWithSample()
        {
            bool timedout = false;
            ManualResetEvent waitToStart = new ManualResetEvent(false);
            ManualResetEvent waitToComplete = new ManualResetEvent(false);

            SmartThreadPool stp = new SmartThreadPool();
            IWorkItemResult wir = stp.QueueWorkItem(
                new WorkItemInfo() { Timeout = 500 },
                state =>
                {
                    waitToStart.Set();
                    Thread.Sleep(1000);
                    timedout = SmartThreadPool.IsWorkItemCanceled;
                    waitToComplete.Set();
                    return null;
                });

            waitToStart.WaitOne();

            waitToComplete.WaitOne();

            Assert.IsTrue(timedout);

            stp.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);
		} 
        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            if (_running)
            {
                _wakeupEvent.Set();
                _stp.Cancel();
                _stp.Shutdown(false, 5000);
                _stp = null;
                btnStart.Content = "Start";
                //txtIdleTimeout.Enabled = true;
                _running = false;
            }
            else
            {
                usageHistoryControl1.Reset();
                usageControl1.Value1 = 0;
                usageControl1.Value2 = 0;
                _wakeupEvent.Set();
                _running = true;
                btnStart.Content = "Stop";
                //txtIdleTimeout.Enabled = false;
                STPStartInfo stpStartInfo = new STPStartInfo()
                {
                    MinWorkerThreads = Convert.ToInt32(txtMinThreads.Text),
                    MaxWorkerThreads = Convert.ToInt32(txtMaxThreads.Text),
                    IdleTimeout = Convert.ToInt32(txtIdleTimeout.Text) * 1000,
                    EnableLocalPerformanceCounters = true,
                };

                _stp = new SmartThreadPool(stpStartInfo);
            }
        }
示例#19
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Sid.WebServices.WebService"/> class.
        /// </summary>
        /// <param name="sidModuleAssemblies">Names of assemblies to scan for SidModules.</param>
        /// <param name="successHandler">SuccessHandler; this is a delegate that, if set, 
        /// executes as the last thing before the response hits the wire.  It allows users to
        /// set response headers.</param>
        /// <param name="prefixes">The set of prefixes for the HttpListener class that Sid 
        /// wraps.</param>
        public WebService(IEnumerable<Assembly> sidModuleAssemblies, 
            System.Func<IRequest, Uri, bool> IsValidOriginHandler, params string[] prefixes)
        {
            SetupJsonDefaults();

            if (!HttpListener.IsSupported) {
                throw new NotSupportedException("Not supported on this");
            }

            threadPool = new SmartThreadPool();

            if (prefixes == null || prefixes.Length == 0) {
                throw new ArgumentException("prefixes");
            }

            foreach (var s in prefixes) {
                listener.Prefixes.Add(s);
            }

            methodDispatcher = new RestfulMethodDispatcher(sidModuleAssemblies) {
                IsValidOrigin = IsValidOriginHandler,
            };

            listener.Start();
        }
        public void DisposeCallerState() 
        { 
            STPStartInfo stpStartInfo = new STPStartInfo();
            stpStartInfo.DisposeOfStateObjects = true;
     
            SmartThreadPool smartThreadPool = new SmartThreadPool(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();
        } 
        public void TwoWIGsStartSuspended()
        {
            SmartThreadPool stp = new SmartThreadPool();

            WIGStartInfo wigStartInfo = new WIGStartInfo();
            wigStartInfo.StartSuspended = true;

            IWorkItemsGroup wig1 = stp.CreateWorkItemsGroup(10, wigStartInfo);
            IWorkItemsGroup wig2 = stp.CreateWorkItemsGroup(10, wigStartInfo);

            wig1.QueueWorkItem(new WorkItemCallback(this.DoWork));
            wig2.QueueWorkItem(new WorkItemCallback(this.DoWork));

            Assert.IsFalse(wig1.WaitForIdle(200));
            Assert.IsFalse(wig2.WaitForIdle(200));

            wig1.Start();

            Assert.IsTrue(wig1.WaitForIdle(200));
            Assert.IsFalse(wig2.WaitForIdle(200));

            wig2.Start();

            Assert.IsTrue(wig1.WaitForIdle(0));
            Assert.IsTrue(wig2.WaitForIdle(200));
        }
        public void STPAndWIGStartSuspended()
        {
            STPStartInfo stpStartInfo = new STPStartInfo();
            stpStartInfo.StartSuspended = true;

            SmartThreadPool stp = new SmartThreadPool(stpStartInfo);

            WIGStartInfo wigStartInfo = new WIGStartInfo();
            wigStartInfo.StartSuspended = true;

            IWorkItemsGroup wig = stp.CreateWorkItemsGroup(10, wigStartInfo);

            wig.QueueWorkItem(new WorkItemCallback(this.DoWork));

            Assert.IsFalse(wig.WaitForIdle(200));

            wig.Start();

            Assert.IsFalse(wig.WaitForIdle(200));

            stp.Start();

            Assert.IsTrue(wig.WaitForIdle(5000), "WIG is not idle");
            Assert.IsTrue(stp.WaitForIdle(5000), "STP is not idle");
        }
        public void Init()
        {
            _stp = new SmartThreadPool();

            WIGStartInfo wigStartInfo = new WIGStartInfo();
            wigStartInfo.FillStateWithArgs = true;
            _wig = _stp.CreateWorkItemsGroup(10, wigStartInfo);
        }
        public static void Main()
        {
            SmartThreadPool stp = new SmartThreadPool();
            stp.OnThreadInitialization += OnInitialization;
            stp.OnThreadTermination += OnTermination;

            stp.QueueWorkItem(DoSomeWork);
        }
示例#25
0
        public void Setup()
        {
            _threadPool = new SmartThreadPool(10 * 1000, MaxWorkerThreads, 0);

            //startInfo.
            //_threadPool = new SmartThreadPool();
            //_threadPool
        }
示例#26
0
 public static void Shutdown()
 {
     if (Pool != null)
     {
         Pool.Shutdown();
         Pool = null;
     }
 }
示例#27
0
        public DownloadManager()
        {
            _webHelper = new WebHelper();

            DownLoadThreadPool = new SmartThreadPool();

            _downloadJobDict = HPPClient.DownloadJobDict;
        }
示例#28
0
        public BaseApproach(int autoCompleteAfterNChars, StandardEvalOutput evalOutput, PrefixProfile queryPrefixProfile)
        {
            _autoCompleteAfterNChars = autoCompleteAfterNChars;
            _evalOutput = evalOutput;
            _queryPrefixProfile = queryPrefixProfile;

            _evalThreadPool = new SmartThreadPool(1000, 6);
            _evalThreadPool.Start(); // Setup and start the threadpool
        }
示例#29
0
 public KmlListener(SerialCommunication serial)
 {
     _smartThreadPool = new SmartThreadPool();
     serial_comm = serial;
     serial.AttitudeCommunicationReceived += new SerialCommunication.ReceiveAttitudeCommunicationFrame(serial_AttitudeCommunicationReceived);
     serial.GpsBasicCommunicationReceived += new SerialCommunication.ReceiveGpsBasicCommunicationFrame(serial_GpsBasicCommunicationReceived);
     serial.PressureTempCommunicationReceived += new SerialCommunication.ReceivePressureTempCommunicationFrame(serial_PressureTempCommunicationReceived);
     serial.ControlInfoCommunicationReceived += new SerialCommunication.ReceiveControlInfoCommunicationFrame(serial_ControlInfoCommunicationReceived);
 }
        static PICSProductInfo()
        {
            ProcessedApps = new Dictionary<uint, IWorkItemResult>();
            ProcessedSubs = new Dictionary<uint, IWorkItemResult>();

            ProcessorThreadPool = new SmartThreadPool();
            ProcessorThreadPool.Concurrency = 50;
            ProcessorThreadPool.Name = "App/Sub Thread Pool";
        }
示例#31
0
                public ICancellable Run(Action action)
                {
                    if (_pool == null)
                    {
                        _pool = new Amib.Threading.SmartThreadPool(new Amib.Threading.STPStartInfo {
                            MaxWorkerThreads               = OptionNumberOfThreads,
                            AreThreadsBackground           = true,
                            EnableLocalPerformanceCounters = false,
                            WorkItemPriority               = Amib.Threading.WorkItemPriority.BelowNormal
                        });
                    }

                    return(new CancellableWrapper(_pool.QueueWorkItem(new Amib.Threading.Action(action))));
                }
示例#32
0
 protected AppSelfHostBase(string serviceName, string handlerPath, params Assembly[] assembliesWithServices)
     : base(serviceName, handlerPath, assembliesWithServices)
 {
     threadPoolManager = new Amib.Threading.SmartThreadPool(IdleTimeout,
                                                            maxWorkerThreads: Math.Max(16, Environment.ProcessorCount * 2));
 }
 protected AppHostHttpListenerSmartPoolBase(string serviceName, string handlerPath, int poolSize, params Assembly[] assembliesWithServices)
     : base(serviceName, handlerPath, assembliesWithServices)
 {
     threadPoolManager = new Amib.Threading.SmartThreadPool(IdleTimeout, poolSize);
 }
 public void Clean()
 {
     WorkThread = null;
     _associatedSmartThreadPool = null;
 }
 public ThreadEntry(SmartThreadPool stp)
 {
     _associatedSmartThreadPool = stp;
     _creationTime  = DateTime.UtcNow;
     _lastAliveTime = DateTime.MinValue;
 }