예제 #1
0
        public bool Shutdown(bool force)
        {
            _forceShutDown   = force;
            _ongoingShutdown = true;

            return(_transportPool.All(transport => transport.Shutdown(force)));
        }
예제 #2
0
        public void ConvertStringWithMultipleThreads()
        {
            BlockingCollection <byte[]> dta = new BlockingCollection <byte[]>();
            int threadIterations            = 50;

            byte[] resultBuffer = null;
            string htmlString   = @"<html><head><title>Test Page</title></head><body><b>COUNTER: {0}</b></body></html>";

            StandardPdfConverterSettings settings = new StandardPdfConverterSettings();

            settings.GlobalSettings               = new PdfConverterGlobalSettings();
            settings.GlobalSettings.Copies        = 1;
            settings.GlobalSettings.DocumentTitle = "YaYa!!!";
            settings.GlobalSettings.Orientation   = Orientation.Portrait;

            DotNetCorePdf pdf = DotNetCorePdf.Create();

            Parallel.For(0, threadIterations, index =>
            {
                string ns = string.Format(htmlString, index);

                using (StandardPdfConverter pdfConverter = pdf.CreateStandardPdfConverter())
                {
                    resultBuffer = pdfConverter.Convert(settings, htmlString);
                    dta.Add(resultBuffer);
                }
            });

            Assert.AreEqual(threadIterations, dta.Count);
            Assert.IsTrue(dta.All(t => t.Length > 0));
        }
예제 #3
0
        public void ConvertBytesWithMultipleThreads()
        {
            BlockingCollection <byte[]> dta = new BlockingCollection <byte[]>();
            int threadIterations            = 2;

            byte[] resultBuffer = null;
            string htmlString   = @"<html><head><title>Test Page</title></head><body><b>COUNTER: {0}</b></body></html>";

            StandardPdfConverterSettings settings = new StandardPdfConverterSettings();

            settings.GlobalSettings               = new PdfConverterGlobalSettings();
            settings.GlobalSettings.Copies        = 1;
            settings.GlobalSettings.DocumentTitle = "YaYa!!!";
            settings.GlobalSettings.Orientation   = Orientation.Portrait;

            DotNetCorePdf pdf = DotNetCorePdf.Create();

            //for (int index = 0; index < threadIterations; index++)
            //{
            //    string ns = string.Format(htmlString, index);
            //    byte[] htmlBuffer = Encoding.UTF8.GetBytes(ns);

            //    using (StandardPdfConverter pdfConverter = pdf.CreateStandardPdfConverter())
            //    {
            //        resultBuffer = pdfConverter.Convert(settings, htmlBuffer);
            //        dta.Add(resultBuffer);
            //    }
            //    using (FileStream fs = new FileStream($@"c:\temp\test_pdf_{index}.pdf", FileMode.Create))
            //    {
            //        fs.Write(resultBuffer, 0, resultBuffer.Length);
            //    }
            //}
            Parallel.For(0, threadIterations, index =>
            {
                System.Threading.Thread.Sleep(2000 * index);
                string ns         = string.Format(htmlString, index);
                byte[] htmlBuffer = Encoding.UTF8.GetBytes(ns);

                using (StandardPdfConverter pdfConverter = pdf.CreateStandardPdfConverter())
                {
                    resultBuffer = pdfConverter.Convert(settings, htmlBuffer);
                    dta.Add(resultBuffer);
                }
                using (FileStream fs = new FileStream($@"c:\temp\test_pdf_{index}.pdf", FileMode.Create))
                {
                    fs.Write(resultBuffer, 0, resultBuffer.Length);
                }
            });

            Assert.AreEqual(threadIterations, dta.Count);
            Assert.IsTrue(dta.All(t => t.Length > 0));
        }
예제 #4
0
        /// <summary>
        /// Get a list of prime numbers excluding the limit in parallel
        /// using a blocking collection.
        /// </summary>
        /// <param name="limit"></param>
        /// <returns></returns>
        public IEnumerable <int> GetPrimesUpToParallel(int limit)
        {
            var blockingCollection = new BlockingCollection <int>()
            {
                2, 3
            };

            Parallel.For(5, limit, (i) =>
            {
                if (blockingCollection.All(p => i % p != 0) &&
                    CalculatePrime(i))
                {
                    blockingCollection.Add(i);
                }
            });
            return(blockingCollection);
        }
        private void ProcessResource(string sport, IResourceFacade resource)
        {
            _logger.DebugFormat("Attempt to process {0} for sport={1}", resource, sport);

            _cachedResources.AddOrUpdate(resource.Id, resource, (k, v) => v);

            // make sure that the resource is not already being processed by some other thread
            if (!_streamManager.TryLockProcessing(resource.Id))
            {
                return;
            }

            bool addedToQueue = false;

            try
            {
                _logger.InfoFormat("Processing {0}", resource);

                if (_streamManager.ShouldProcessResource(resource))
                {
                    if (_resourceCreationQueue.All(x => x.Id != resource.Id))
                    {
                        _logger.DebugFormat("Adding {0} to the creation queue ", resource);
                        _resourceCreationQueue.Add(resource);
                        addedToQueue = true;
                        _logger.DebugFormat("Added {0} to the creation queue", resource);
                    }
                    else
                    {
                        _logger.DebugFormat("{0} is already added to the creation queue ", resource);
                    }
                }
            }
            finally
            {
                if (!addedToQueue)
                {
                    _streamManager.ReleaseProcessing(resource.Id);
                }
            }
        }
 private bool IsPrime(int val)
 {
     return(_primes.All(p => (val % p != 0)));
 }
예제 #7
0
 /// <summary>
 /// Determines whenever importer is disabled
 /// </summary>
 /// <returns>True if importer is disabled; otherwise - false</returns>
 public override bool IsDisabled()
 {
     return(importers.All(x => x.IsDisabled()));
 }
        public void BlockingCollectionAddToAnyTest()
        {
            var depots = new BlockingCollection <int> [5];

            for (int i = 0; i < depots.Length; i++)
            {
                depots[i] = new BlockingCollection <int>(10);
            }

            var tokenSource = new CancellationTokenSource();
            var token       = tokenSource.Token;

            var times     = 100;
            var writeTask = Task.Factory.StartNew(() =>
            {
                for (int i = 0; i < times; i++)
                {
                    try
                    {
                        var timeout = 1;
                        var index   = BlockingCollection <int> .TryAddToAny(depots, i, timeout, token);
                        if (index < 0)
                        {
                            Trace.WriteLine($"time out. item: {i}");
                            Thread.Sleep(1);
                        }
                        else
                        {
                            Trace.WriteLine($"index: {index}, insert item: {i}");
                        }
                    }
                    catch (OperationCanceledException ex)
                    {
                        Trace.WriteLine("operation was canceled.");
                        break;
                    }
                }

                foreach (var blockingCollection in depots)
                {
                    blockingCollection.CompleteAdding();
                }
            }, token);

            var readTask = Task.Factory.StartNew(() =>
            {
                while (!token.IsCancellationRequested && depots.All(o => !o.IsCompleted))
                {
                    int value;
                    BlockingCollection <int> .TakeFromAny(depots, out value);
                    Trace.WriteLine($"get item: {value}");
                    Thread.Sleep(1);
                }
            }, token);

            try
            {
                Task.WaitAll(writeTask, readTask);
            }
            catch (AggregateException ex)
            {
                foreach (var innerException in ex.InnerExceptions)
                {
                    Trace.WriteLine(innerException.Message);
                }
            }
        }