public void StartSuspended()
        {
            STPStartInfo stpStartInfo = new STPStartInfo();
            stpStartInfo.StartSuspended = true;

            SmartThreadPool stp = new SmartThreadPool(stpStartInfo);

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

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

            stp.Start();

            Assert.IsTrue(stp.WaitForIdle(200));
        }
        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 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 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 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 GoodCallback()
        {
            SmartThreadPool stp = new SmartThreadPool();

            stp.QueueWorkItem(new WorkItemCallback(DoWork));

            stp.WaitForIdle();

            stp.Shutdown();
        }
        /// <summary>
        /// Starts the tasks execution.
        /// </summary>
        /// <returns>If has reach the timeout false, otherwise true.</returns>
        public override bool Start()
        {
            base.Start();
            m_threadPool = new SmartThreadPool();

            try
            {
                m_threadPool.MinThreads = MinThreads;
                m_threadPool.MaxThreads = MaxThreads;
                var workItemResults = new IWorkItemResult[Tasks.Count];

                for (int i = 0; i < Tasks.Count; i++)
                {
                    var t = Tasks[i];
                    workItemResults[i] = m_threadPool.QueueWorkItem(new WorkItemCallback(Run), t);
                }

                m_threadPool.Start();

                // Timeout was reach?
                if (!m_threadPool.WaitForIdle(Timeout.TotalMilliseconds > int.MaxValue ? int.MaxValue : Convert.ToInt32(Timeout.TotalMilliseconds)))
                {
                    if (m_threadPool.IsShuttingdown)
                    {
                        return true;
                    }
                    else
                    {
                        m_threadPool.Cancel(true);
                        return false;
                    }
                }

                foreach (var wi in workItemResults)
                {
                    Exception ex;
                    wi.GetResult(out ex);

                    if (ex != null)
                    {
                        throw ex;
                    }
                }

                return true;
            }
            finally
            {
                m_threadPool.Shutdown(true, 1000);
                m_threadPool.Dispose();
                IsRunning = false;
            }
        }
        public void WaitForIdle()
        {
            SmartThreadPool smartThreadPool = new SmartThreadPool(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 Character Parse(Character character, bool force)
        {
            stopwatch.Reset();
            Stopwatch watch = new Stopwatch();
            watch.Start();

            _current = character;

            // Only parse the character once a day unless forced
            if (character.LastParseDate.HasValue &&
                character.LastParseDate.Value.AddDays(7) > DateTime.Now && !force)
            {
                return character;
            }

            ParseCharacterInformation(character);

            //character.Achievements.Clear();

            string mainAchievementPageUrl = string.Format("http://{2}.battle.net/wow/en/character/{0}/{1}/achievement", character.Server, character.Name, character.Region);
            HtmlDocument doc = DownloadPage(mainAchievementPageUrl);
            List<AchievedAchievement> achievements = new List<AchievedAchievement>();

            ProcessPageForAchievements(doc.DocumentNode, character);
            pagesToParse = new List<HtmlDocument>();
            IList<string> extraPages = FindSubAchievementPages(doc.DocumentNode);

            SmartThreadPool pool = new SmartThreadPool();
            foreach (string pageUrl in extraPages)
            {
                pool.QueueWorkItem(new WorkItemCallback(ProcessPageOnNewThread), pageUrl);
            }

            pool.Start();
            pool.WaitForIdle();

            try
            {
                foreach (HtmlDocument page in pagesToParse)
                {
                    ProcessPageForAchievements(page.DocumentNode, character);
                }
            }
            catch (IndexOutOfRangeException)
            {
            }
            character.LastParseDate = DateTime.Now;
            character.CurrentPoints = character.TotalAchievementPoints;
            watch.Stop();

            return character;
        }
        public void GoodPostExecute()
        {
            SmartThreadPool stp = new SmartThreadPool();

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

            stp.WaitForIdle();

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

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

            stp.QueueWorkItem(workItemCallback);

            stp.WaitForIdle();

            stp.Shutdown();
        }
	    private static void CheckApartmentState(ApartmentState requestApartmentState)
	    {
	        STPStartInfo stpStartInfo = new STPStartInfo();
            stpStartInfo.ApartmentState = requestApartmentState;

	        SmartThreadPool stp = new SmartThreadPool(stpStartInfo);

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

	        ApartmentState resultApartmentState = wir.GetResult();

	        stp.WaitForIdle();

	        Assert.AreEqual(requestApartmentState, resultApartmentState);
	    }
		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();
		} 
        private static void CheckIsBackground(bool isBackground)
	    {
	        STPStartInfo stpStartInfo = new STPStartInfo();
	        stpStartInfo.AreThreadsBackground = isBackground;

	        SmartThreadPool stp = new SmartThreadPool(stpStartInfo);

            IWorkItemResult<bool> wir = stp.QueueWorkItem(() => GetCurrentThreadIsBackground());

	        bool resultIsBackground = wir.GetResult();

	        stp.WaitForIdle();

            Assert.AreEqual(isBackground, resultIsBackground);
	    }
        public void ChainedDelegatesPostExecute()
        {
            SmartThreadPool stp = new SmartThreadPool();

            PostExecuteWorkItemCallback postExecuteWorkItemCallback = DoPostExecute;
            postExecuteWorkItemCallback += DoPostExecute;

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

            stp.WaitForIdle();

            stp.Shutdown();
        }
Example #16
0
        public void TestPipe()
        {
            SafeCounter sc = new SafeCounter();
            SmartThreadPool stp = new SmartThreadPool();

            stp.Pipe(
                sc,
                sc1 => { if (sc.Counter == 0) { sc1.Increment(); }},
                sc1 => { if (sc.Counter == 1) { sc1.Increment(); }},
                sc1 => { if (sc.Counter == 2) { sc1.Increment(); }}
                );

            stp.WaitForIdle ();
            Assert.AreEqual(3, sc.Counter);

            stp.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 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();
        }
		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 ChainedDelegatesPostExecute()
        {
            Assert.Throws<NotSupportedException>(() =>
            {

                SmartThreadPool stp = new SmartThreadPool();

                PostExecuteWorkItemCallback postExecuteWorkItemCallback = DoPostExecute;
                postExecuteWorkItemCallback += DoPostExecute;

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

                stp.WaitForIdle();

                stp.Shutdown();
            });
        }
        public void DoWork(object [] states)
        {
            STPStartInfo stpStartInfo = new STPStartInfo();
            stpStartInfo.StartSuspended = true;

            SmartThreadPool smartThreadPool = new SmartThreadPool(stpStartInfo);

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

            // Start working on the work items in the queue
            smartThreadPool.Start();

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

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

                bool hasRun = false;

                SmartThreadPool stp = new SmartThreadPool(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 DoWork()
        {
            STPStartInfo stpStartInfo = new STPStartInfo();
            stpStartInfo.StartSuspended = true;

            SmartThreadPool smartThreadPool = new SmartThreadPool();

            smartThreadPool.QueueWorkItem(
                new WorkItemCallback(this.DoSomeWork),
                "Queued first",
                WorkItemPriority.BelowNormal);

            smartThreadPool.QueueWorkItem(
                new WorkItemCallback(this.DoSomeWork),
                "Queued second",
                WorkItemPriority.AboveNormal);

            smartThreadPool.Start();

            smartThreadPool.WaitForIdle();

            smartThreadPool.Shutdown();
        }
Example #26
0
        public void Cancel1WIGof2WorkItems()
        {
            int counter1 = 0;
            int counter2 = 0;

            SmartThreadPool stp = new SmartThreadPool();
            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();
        }
        public bool Sync()
        {
            if (syncDirection == SynchronizeDirection.Upload)
            {
                if (logger != null) logger.Log("Start Synchronizer Enlisting Files");
                List<string> fileList = AzureHelper.ListFiles(localSource, this.indexFileName, this.dataFileName);
                List<string> fileListToUpload = new List<string>(fileList);
                if (logger != null) logger.Log("End Synchronizer Enlisting Files");
                // todo filter list, to exclude archival stream files.
                bool retVal = true;

                foreach (string file in fileList)
                {
                    if (Path.GetFileName(file).Equals(this.indexFileName))
                    {
                        if (logger != null) logger.Log("Start Synchronizer Upload FileSimple");
                        retVal = amazonS3Helper.UploadFileToS3Object(indexFileName, file);
                        if (logger != null) logger.Log("End Synchronizer Upload FileSimple");
                        fileListToUpload.Remove(file);
                    }
                    else if (Path.GetFileName(file).Equals(this.dataFileName))
                    {
                        if (logger != null) logger.Log("Start Synchronizer Upload FileAsChunks");
                        chunkListHash = amazonS3Helper.UploadFileAsChunks(file);
                        if (logger != null) logger.Log("End Synchronizer Upload FileAsChunks");
                        retVal = (chunkListHash == null) ? false : true;
                        fileListToUpload.Remove(file);
                    }

                    if (SyncFactory.FilesExcludedFromSync.Contains(Path.GetFileName(file)) || SyncFactory.FilesExcludedFromSync.Contains(Path.GetExtension(file)))
                    {
                        // AzureHelper.structuredLog("I", "Ignoring sync for file {0}" , file);
                        // do nothing. just ignore the file
                        fileListToUpload.Remove(file);
                    }
                } //we've taken care of the index, data, and files to ignore. whatever is left are datablock files for file data streams (?)


                if (logger != null) logger.Log("Start Synchronizer Upload FDSFiles");
                SmartThreadPool threadPool = new SmartThreadPool();
                threadPool.MaxThreads = this.MaxConcurrentFileSyncThreads;
                foreach (string file in fileListToUpload)
                {
                    IWorkItemResult wir1 = threadPool.QueueWorkItem(new WorkItemCallback(this.UploadFileToBlockBlob_worker), file);
                }
                threadPool.Start();
                threadPool.WaitForIdle();
                threadPool.Shutdown();
                if (logger != null) logger.Log("End Synchronizer Upload FDSFiles");
            }

            if (syncDirection == SynchronizeDirection.Download)
            {
                this.GetDataFileMetadata();// get chunkMD and populate chunkList Hash
                return amazonS3Helper.DownloadS3ObjectToFile(indexFileName, localSource + "/" + indexFileName);// download index 
            }

            return true;//TODO fix this to return correct value for upload cases
        }
Example #28
0
        private void Main_Load(object sender, EventArgs e)
        {
            // 开启wcf服务
            this.SpiderWCFNetPipe();
            this.Report();

            // 开启线程启动浏览器进程
            // this.StartWebBrower();

            var p1 = SingletonProvider<ProcessWatcher>.UniqueInstance;
            p1.StartWatch();
            return;
            //TestSqlite();
            var dt = DataAccess.GetProductCategory(" ECPlatformId=4 limit 48,60");
            var urls = new List<string>();
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    if (!dr.IsNull("Url"))
                    {
                        var url = Convert.ToString(dr["Url"]);
                        //Spider.SuNingProductList(url);
                        urls.Add(url);
                    }
                }
            }
            System.Net.ServicePointManager.DefaultConnectionLimit = 768;

            SmartThreadPool smartThreadPool = new SmartThreadPool(1000 * 1000, 10);

            foreach (string state in urls)
            {
                smartThreadPool.QueueWorkItem(new WorkItemCallback(Spider.SuNingProductList), state);
            }

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

            smartThreadPool.Shutdown();

            MessageBox.Show("采集成功");
        }
        public void TimeoutInProgressWorkItemSoftWithAbortOnWorkItemCancel()
        {
            bool abortFailed = 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);
                    SmartThreadPool.AbortOnWorkItemCancel();
                    abortFailed = true;
                    return null;
                });

            waitToStart.WaitOne();

            stp.WaitForIdle();

            Assert.IsTrue(wir.IsCanceled);
            Assert.IsFalse(abortFailed);
            stp.Shutdown();
        }
Example #30
0
        public void CancelInProgressWorkItemSoftWithSample()
        {
            bool cancelled = false;
            ManualResetEvent waitToStart = new ManualResetEvent(false);
            ManualResetEvent waitToComplete = new ManualResetEvent(false);

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

            waitToStart.WaitOne();

            wir.Cancel(false);

            waitToComplete.Set();

            stp.WaitForIdle();

            Assert.IsTrue(cancelled);

            stp.Shutdown();
        }