Пример #1
0
 public Data()
 {
     _cnt = "";
     Reviewes = new System.Collections.Concurrent.ConcurrentBag<Review>();
     Country = "Indonesia";
     City = "Bali";
 }
Пример #2
0
 public static Module[] LoadModule(SelectModules selectModules, bool start = false)
 {
     try
     {
         if (selectModules == SelectModules.New)
         {
             string[] files = Directory.GetFiles(@"Modules/");
             System.Collections.Concurrent.ConcurrentBag <Module> ret = new System.Collections.Concurrent.ConcurrentBag <Module>();
             Parallel.ForEach(files, (filePath) =>
             {
                 if (filePath.EndsWith(".module.dll"))
                 {
                     Parallel.ForEach(LoadModule(filePath, start), (module) =>
                     {
                         ret.Add(module);
                     });
                 }
             });
             if (ret.Count == 0)
             {
                 throw new Exception("None of the module files marked as new are refering to any modules!");
             }
             return(ret.ToArray());
         }
         else
         {
             throw new Exception("You can load only new or specified modules. You can also remove or reload them reloading them.");
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
         return(null);
     }
 }
Пример #3
0
        /// <summary>
        /// Adds the scoped Disposable object to the scope.
        /// </summary>
        /// <remarks>All objects added here will be disposed of when the scope is disposed</remarks>
        /// <param name="disposable">The disposable.</param>
        public void AddScopedObject(IDisposable disposable)
        {
            if (_disposables == null)
                _disposables = new System.Collections.Concurrent.ConcurrentBag<IDisposable>();

            _disposables.Add(disposable);
        }
Пример #4
0
        static void Main(string[] args)
        {
            var watch = new Stopwatch();

            watch.Start();

            System.Collections.Concurrent.ConcurrentBag <Tuple <long, decimal> > lista = new System.Collections.Concurrent.ConcurrentBag <Tuple <long, decimal> >();
            ServicePointManager.DefaultConnectionLimit = 10000;

            //Mejora: Parametros parametrizables en variables.
            Enumerable.Range(0, totalLength / (int)blockSize).ParallelForEachAsync(
                async(x) =>
            {
                var start = x * blockSize;

                lista.Add(await Hilo(start, x));
            }, 16).Wait();


            watch.Stop();

            var total1 = lista.Sum(x => x.Item1);
            var total2 = lista.Sum(x => x.Item2);

            Console.WriteLine($"Total lineas: {total1}");
            Console.WriteLine($"Media: {total2 / total1}");
            Console.WriteLine($"Han pasado {watch.Elapsed.Minutes}:{watch.Elapsed.Seconds} segundos");

            Console.ReadKey();
        }
Пример #5
0
        private async void Form1_OnLoad(object sender, EventArgs e)
        {
            while (true)
            {
                var ringReq = WebRequest.Create(frontendUri);
                ringReq.Method = "DIAG";
                RingNode[] ringNodeArray;
                using (var ringResp = await ringReq.GetResponseAsync())
                {
                    var ringNodeSerializer = new XmlSerializer(typeof(RingNode[]), new XmlRootAttribute { ElementName = "Ring" });
                    ringNodeArray = (RingNode[])ringNodeSerializer.Deserialize(ringResp.GetResponseStream());
                }
                var allData = new System.Collections.Concurrent.ConcurrentBag<Tuple<int, string, byte[]>>();
                Parallel.ForEach(ringNodeArray, node =>
                {
                    var req = WebRequest.Create(node.NodeUri);
                    req.Method = "DIAG";

                    using (var resp = req.GetResponse())
                    {
                        var kvpsSerializer = new XmlSerializer(typeof(SerializableKeyValuePair));
                        var tmpKvps = kvpsSerializer.Deserialize(resp.GetResponseStream()) as SerializableKeyValuePair[];
                        foreach (var kvp in tmpKvps)
                        {
                            allData.Add(new Tuple<int, string, byte[]>(node.RingId, kvp.Key, kvp.Value));
                        }
                    }
                });

                this.listView1.Items.Clear();
                this.listView1.Items.AddRange(allData.Select(t => 
                    new ListViewItem(new string[] {t.Item1.ToString(), t.Item2, Encoding.Default.GetString(t.Item3)})).ToArray());
            }
        }
Пример #6
0
		public void TablesUniquelyNamedOnlyWithinThread()
		{
			var uniqueIntegerList = new System.Collections.Concurrent.ConcurrentBag<int>();
			var method = new ThreadStart(() =>
			                             {
				                             Table tbl1 = new Table();
				                             Table tbl2 = new Table();

				                             // Store these values for later comparison
				                             uniqueIntegerList.Add(tbl1.UniqueInteger);
				                             uniqueIntegerList.Add(tbl2.UniqueInteger);

				                             // Ensure that within a thread we have unique integers
				                             Assert.AreEqual(tbl1.UniqueInteger + 1, tbl2.UniqueInteger);
			                             });

			var thread1 = new CrossThreadTestRunner(method);
			var thread2 = new CrossThreadTestRunner(method);

			thread1.Start();
			thread2.Start();

			thread1.Join();
			thread2.Join();

			// There should in total be 4 tables, but only two distinct identifiers.
			Assert.AreEqual(4, uniqueIntegerList.Count);
			Assert.AreEqual(2, uniqueIntegerList.Distinct().Count());
		}
Пример #7
0
        static async public Task <ConcurrentBag <CalendarEvent> > getAllCalEvents(IEnumerable <GoogleTilerEventControl> AllGoogleCalControl, TimeLine CalculationTimeLine, bool retrieveLocationFromGoogle = false)
        {
            ConcurrentBag <List <CalendarEvent> >         RetValueListContainer = new System.Collections.Concurrent.ConcurrentBag <List <CalendarEvent> >();
            ConcurrentBag <Task <List <CalendarEvent> > > ConcurrentTask        = new System.Collections.Concurrent.ConcurrentBag <System.Threading.Tasks.Task <List <TilerElements.CalendarEvent> > >();
            ConcurrentBag <CalendarEvent> RetValue = new System.Collections.Concurrent.ConcurrentBag <CalendarEvent>();

            //AllGoogleCalControl.AsParallel().ForAll(obj=>

            foreach (GoogleTilerEventControl obj in AllGoogleCalControl)
            {
                ConcurrentTask.Add(obj.getCalendarEventsForIndex(CalculationTimeLine, false));
            }
            //);

            /*
             * Parallel.ForEach(ConcurrentTask, async EachTask =>
             *  {
             *      List<CalendarEvent> ALlCalEvents = await EachTask.ConfigureAwait(false);
             *      ALlCalEvents.ForEach(obj1 => RetValue.Add(obj1));
             *  }
             *  );
             */


            foreach (Task <List <CalendarEvent> > EachTask in ConcurrentTask)
            {
                List <CalendarEvent> ALlCalEvents = await EachTask.ConfigureAwait(false);

                ALlCalEvents.ForEach(obj1 => RetValue.Add(obj1));
            }

            return(RetValue);
        }
        /// <inheritdoc />
        public async Task <IReadOnlyCollection <Player> > GetCleanableDirtyPlayersAsync()
        {
            var okPlayers = new System.Collections.Concurrent.ConcurrentBag <Player>();

            var players = await _readRepository
                          .GetDirtyPlayersAsync(false)
                          .ConfigureAwait(false);

            const int parallel = 8;

            for (var i = 0; i < players.Count; i += parallel)
            {
                await Task.WhenAll(players.Skip(i).Take(parallel).Select(async p =>
                {
                    var pInfo = await _siteParser
                                .GetPlayerInformationAsync(p.UrlName, Player.DefaultPlayerHexColor)
                                .ConfigureAwait(false);

                    if (pInfo != null)
                    {
                        pInfo.Id = p.Id;
                        okPlayers.Add(new Player(pInfo));
                    }
                })).ConfigureAwait(false);
            }

            return(okPlayers);
        }
Пример #9
0
        /// <summary>
        /// Asynchronously converts all of the .doc files it receives into .docx files
        /// If no arguments are supplied, it will instead convert all yet unconverted .doc files in the project directory
        /// Results are stored in corresponding project directory
        /// </summary>
        /// <param name="files">0 or more instances of the DocFile class which encapsulate .doc files.</param>
        private static async Task <IEnumerable <TxtFile> > ConvertDocToTextAsync(IEnumerable <DocFile> files)
        {
            ThrowIfUninitialized();
            var convertedFiles = new System.Collections.Concurrent.ConcurrentBag <TxtFile>();

            foreach (var document in files.Except <InputFile>(taggedFiles))
            {
                try
                {
                    var docx  = await new DocToDocXConverter(document as DocFile).ConvertFileAsync();
                    var txt   = await new DocxToTextConverter(docx as DocXFile).ConvertFileAsync();
                    var added = AddFile(txt);
                    convertedFiles.Add(added);
                    File.Delete(txt.FullPath);
                    File.Delete(docx.FullPath);
                }
                catch (IOException e)
                {
                    LogConversionFailure(document, e);
                    throw;
                }
                catch (UnauthorizedAccessException e)
                {
                    LogConversionFailure(document, e);
                    throw;
                }
            }
            return(convertedFiles);
        }
Пример #10
0
        public List <StreamItem> GetStreamItems()
        {
            var list          = new System.Collections.Concurrent.ConcurrentBag <StreamItem>();
            var subscriptions = GetAllSubscriptions();

            Parallel.ForEach(subscriptions, s =>
            {
                var plugin      = GetPluginFromSubscription(s);
                var items       = new List <StreamItem>();
                var cachedItems = Context.Cache[s.ID.ToString()];

                items = cachedItems != null ? (List <StreamItem>)cachedItems : plugin.Execute(s);

                if (items != null && items.Count > 0)
                {
                    Context.Cache.Add(s.ID.ToString(), items.ToList(), null,
                                      DateTime.Now.AddSeconds(CurrentSiteInfo.CacheDuration),
                                      System.Web.Caching.Cache.NoSlidingExpiration,
                                      System.Web.Caching.CacheItemPriority.Normal, null);

                    items.ForEach(item => list.Add(item));
                }
            });

            return(list.Where(i => i != null && i.Timestamp != null)
                   .OrderByDescending(i => i.Timestamp).ToList());
        }
Пример #11
0
        public void TablesUniquelyNamedOnlyWithinThread()
        {
            var uniqueIntegerList = new System.Collections.Concurrent.ConcurrentBag <int>();
            var method            = new ThreadStart(() =>
            {
                Table tbl1 = new Table();
                Table tbl2 = new Table();

                // Store these values for later comparison
                uniqueIntegerList.Add(tbl1.UniqueInteger);
                uniqueIntegerList.Add(tbl2.UniqueInteger);

                // Ensure that within a thread we have unique integers
                Assert.AreEqual(tbl1.UniqueInteger + 1, tbl2.UniqueInteger);
            });

            var thread1 = new CrossThreadTestRunner(method);
            var thread2 = new CrossThreadTestRunner(method);

            thread1.Start();
            thread2.Start();

            thread1.Join();
            thread2.Join();

            // There should in total be 4 tables, but only two distinct identifiers.
            Assert.AreEqual(4, uniqueIntegerList.Count);
            Assert.AreEqual(2, uniqueIntegerList.Distinct().Count());
        }
Пример #12
0
        public async Task <IEnumerable <BundleMetainfo> > ReadBundleMetainfos()
        {
            var list = new System.Collections.Concurrent.ConcurrentBag <BundleMetainfo>();

            pathHelper.PrepareDirectories();
            var baseDir = new DirectoryInfo(pathHelper.GetWorkingDir());

            var sw      = new Stopwatch(); sw.Start();
            var subdirs = baseDir.GetDirectories().OrderByDescending(x => x.CreationTime);

            sw.Stop(); Console.WriteLine($"Getting list of {subdirs.Count()} subdirectories took {sw.Elapsed.TotalSeconds} seconds."); sw.Reset();

            sw.Start();
            var tasks = subdirs.Select(dir => Task.Run(async() => {
                var bundleId         = dir.Name;
                var metainfoFilename = pathHelper.GetBundleMetadataPath(bundleId);
                if (!File.Exists(metainfoFilename))
                {
                    // backwards compatibility, when Metadata files did not exist
                    await CreateBundleMetainfoForCompat(bundleId);

                    list.Add(new BundleMetainfo()
                    {
                        BundleId = bundleId
                    });
                }
                list.Add(ReadMetainfoFile(metainfoFilename));
            }));
            await Task.WhenAll(tasks);

            sw.Stop(); Console.WriteLine($"ReadBundleMetainfos of {subdirs.Count()} bundles took {sw.Elapsed.TotalSeconds} seconds."); sw.Reset();

            return(list);
        }
Пример #13
0
        private static List <IPAddress> GetNetworkInterfaces(bool addLoopback = false)
        {
            var ninterfaces      = NetworkInterface.GetAllNetworkInterfaces();
            var activeInterfaces = new System.Collections.Concurrent.ConcurrentBag <IPAddress>();

            Parallel.ForEach <NetworkInterface>(ninterfaces, new ParallelOptions()
            {
                MaxDegreeOfParallelism = 5
            },
                                                iface =>
            {
                if (iface.OperationalStatus == OperationalStatus.Up)
                {
                    var ipprops = iface.GetIPProperties();
                    if (ipprops.UnicastAddresses != null && ipprops.UnicastAddresses.Count > 0)
                    {
                        var ipV4List = ipprops.UnicastAddresses
                                       .Where(x => x.Address.AddressFamily == AddressFamily.InterNetwork && x.IsDnsEligible)
                                       .ToList();

                        if (ipV4List.Count > 0)
                        {
                            activeInterfaces.Add(ipV4List.First().Address);
                        }
                    }
                }
            });

            if (addLoopback)
            {
                activeInterfaces.Add(IPAddress.Loopback);
            }
            return(activeInterfaces.ToList());
        }
Пример #14
0
        private static void CheckAssertionsInParallel(string directory, string tag, ProgramAttributes attributes)
        {
            //Parallel.For(0, attributes.numSplits,
            //  new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount },
            //  i => CheckAssertion(directory, tag, i, attributes));
            var delim = Options.IsLinux() ? @"/" : @"\";
            List<Tuple<string, string>> solvers = new List<Tuple<string, string>>();
            AddSolver(solvers, "Z3_440", @"/z3exe:", "." + delim + "references" + delim + "z3.4.4.0.exe", "/z3opt:smt.RELEVANCY=0 /z3opt:smt.CASE_SPLIT=0");
            AddSolver(solvers, "Z3_441", @"/z3exe:", "." + delim + "references" + delim + "z3.4.4.1.exe", "/z3opt:smt.RELEVANCY=0 /z3opt:smt.CASE_SPLIT=0");
            AddSolver(solvers, "Z3_441_OPTIMIZE_BV", @"/z3exe:", "." + delim + "references" + delim + "z3.4.4.1.exe", "/proverOpt:OPTIMIZE_FOR_BV=true");

            // work stealing parallel implementation
            workItems = new System.Collections.Concurrent.ConcurrentBag<Tuple<string, string, int, ProgramAttributes>>();
            foreach (Tuple<string, string> solver in solvers)
            {
                attributes.solverName = solver.Item1;
                attributes.solverCallArg = solver.Item2;
                for (int i = 0; i < attributes.numSplits; i++)
                    workItems.Add(Tuple.Create(directory, tag, i, attributes));
            }

            var threads = new List<Thread>();
            for (int i = 0; i < Environment.ProcessorCount * 0.5; i++)
            {
                threads.Add(new Thread(new ThreadStart(CheckAssertions)));
            }

            threads.ForEach(t => t.Start());
            threads.ForEach(t => t.Join());

        }
Пример #15
0
 public ReportData()
 {
     PagesReportData = new System.Collections.Concurrent.ConcurrentBag <PageReportData>();
     PageThreshold   = 0;
     Duration        = TimeSpan.Zero;
     QualityMatch    = new List <double>();
 }
Пример #16
0
        public IEnumerable <TfsChangesetWrapper> QueryChangesets(IEnumerable <TfsWorkItemWrapper> workItems, ITrackProgress trackProgress, CancellationToken cancelled = default(CancellationToken))
        {
            var results = new System.Collections.Concurrent.ConcurrentBag <TfsChangesetWrapper>();

            Parallel.ForEach(workItems,
                             (workItem, state) =>
            {
                if (Repository.Instance.TfsConnectionInfo.Uri == null)
                {
                    state.Stop();
                    return;
                }

                if (cancelled.IsCancellationRequested)
                {
                    state.Stop();
                    return;
                }
                trackProgress.Increment();
                foreach (var changeset in workItem.RelatedChangesets)
                {
                    trackProgress.ProgressInfo = String.Format("Processing work item #{0} ({1} related changesets)", workItem.TfsWorkItem.Id, workItem.RelatedChangesetCount);
                    results.Add(UpdateChangesetFromCache(new TfsChangesetWrapper(changeset)));
                }
            });
            cancelled.ThrowIfCancellationRequested();

            var resultList =
                results
                .GroupBy(item => item.TfsChangeset.Changeset.ChangesetId)
                .Select(item => item.First());

            return(resultList.ToList().AsEnumerable());
        }
Пример #17
0
        /// <summary>
        /// 将组合商品拆分成基础商品信息
        /// </summary>
        public void setProductNum(RequestOrderStatusList m, System.Collections.Concurrent.ConcurrentBag <RequestOrderStatusList> productList, System.Collections.Concurrent.ConcurrentDictionary <string, List <string> > baseProductDictionary)
        {
            var ProductList = this.GetXMProductListByzuheCode(m.PlatformMerchantCode, 508);//判断是否是组合商品,是的话将会拆分成组成商品的基础商品信息
            var erpPlatformMerchantCodelist = new List <string>();

            if (ProductList.Count != 0)
            {
                foreach (var elem in ProductList)
                {
                    var entity = new RequestOrderStatusList();
                    entity.OrderCode            = m.OrderCode;
                    entity.PlatformMerchantCode = elem.PlatformMerchantCode; //料号(商品编码,对应供应商那边)
                    erpPlatformMerchantCodelist.Add(elem.PlatformMerchantCode);
                    productList.Add(entity);
                }
            }
            else
            {
                productList.Add(m);
                //erpPlatformMerchantCodelist.Add(m.PlatformMerchantCode);
            }
            var keyvalue = m.OrderCode + "," + m.PlatformMerchantCode;

            if (!baseProductDictionary.Keys.Contains(keyvalue))
            {
                baseProductDictionary.TryAdd(keyvalue, erpPlatformMerchantCodelist);
            }
        }
Пример #18
0
 public static void Clear <T>(this System.Collections.Concurrent.ConcurrentBag <T> concurrentBag)
 {
     while (concurrentBag.TryTake(out _))
     {
         // Empty
     }
 }
Пример #19
0
        public IEnumerable <TfsWorkItemWrapper> QueryWorkItems(IEnumerable <TfsChangesetWrapper> changesets, ITrackProgress trackProgress, CancellationToken cancelled = default(CancellationToken))
        {
            var results = new System.Collections.Concurrent.ConcurrentBag <TfsWorkItemWrapper>();

            Parallel.ForEach(changesets,
                             (changeset, state) =>
            {
                if (Repository.Instance.TfsConnectionInfo.Uri == null)
                {
                    state.Stop();
                    return;
                }
                if (cancelled.IsCancellationRequested)
                {
                    state.Stop();
                    return;
                }
                foreach (var workItem in changeset.TfsChangeset.RelatedWorkItems)
                {
                    results.Add(UpdateWorkItemFromCache(new TfsWorkItemWrapper(workItem)));
                }
                trackProgress.Increment();
            });
            cancelled.ThrowIfCancellationRequested();

            var resultList =
                results
                .GroupBy(item => item.TfsWorkItem.Id)
                .Select(item => item.First());

            return(resultList.ToList().AsEnumerable());
        }
Пример #20
0
        private static void TestTheadManager()
        {
            System.Collections.Concurrent.ConcurrentBag <string> bag = new System.Collections.Concurrent.ConcurrentBag <string>();

            object locko = new object();
            var    vals  = Enumerable.Range(1, 17).Select(m => m.ToString()).ToArray();

            Devmasters.Batch.ThreadManager.DoActionForAll <string>(vals,
                                                                   i =>
            {
                System.Threading.Thread.Sleep(Devmasters.Core.Rnd.Next(50, 140));
                Console.WriteLine($"{i}");
                lock (locko)
                {
                    bag.Add(i);
                }
                return(new Devmasters.Batch.ActionOutputData());
            }, true, 50,
                                                                   Devmasters.Batch.Manager.DefaultOutputWriter,
                                                                   new Devmasters.Batch.ActionProgressWriter(0.1f).Writer
                                                                   );

            System.IO.File.WriteAllLines(@"c:\!\!xx.txt", bag.OrderBy(m => m).Select(m => m.ToString()));
            return;
        }
Пример #21
0
                    public static List <long> UptoParallel(int limit)
                    {
                        int sieveBound = (int)(limit - 1) / 2;
                        int crossLimit = ((int)(Math.Sqrt(limit) - 1)) / 2;

                        bool[] sieveArray = new bool[sieveBound];

                        Parallel.For(1, crossLimit, (i) =>
                        {
                            if (sieveArray[i] == false)
                            {
                                for (int j = 2 * i * (i + 1); j < sieveBound; j += (2 * i) + 1)
                                {
                                    sieveArray[j] = true;
                                }
                            }
                        });

                        System.Collections.Concurrent.ConcurrentBag <long> sieve = new System.Collections.Concurrent.ConcurrentBag <long>();

                        sieve.Add(2);

                        Parallel.For(1, sieveBound, (i) =>
                        {
                            if (sieveArray[i] == false)
                            {
                                sieve.Add(2 * i + 1);
                            }
                        });

                        return(sieve.AsParallel().OrderBy(c => c).ToList <long>());
                    }
            private IEnumerable <Quadruple> UnaryOp(Operation op, IEnumerable <Quadruple> ps)
            {
                var sda = new System.Collections.Concurrent.ConcurrentBag <byte[]>();



                long i    = 0;
                var  cmax = sb.Length / 64;
                var  c    = 0;

                foreach (var p in ps)
                {
                    var offset = 64 * c;
                    Unsafe.As <byte, Int128>(ref sb[offset + 0]) = default;
                    Unsafe.As <byte, Operation>(ref sb[offset + 0])  = op;
                    Unsafe.As <byte, Quadruple>(ref sb[offset + 16]) = p;
                    Unsafe.As <byte, Int128>(ref sb[offset + 32]) = default;
                    Unsafe.As <byte, Int128>(ref sb[offset + 48]) = default;
                    unchecked {
                        ++c;
                    }
                    unchecked {
                        ++i;
                    }
                    if (cmax == c)
                    {
                        var        a      = nc.Send(sb, 64 * c, "localhost", 16383);
                        IPEndPoint ep     = null;
                        var        sdfasd = nc.Receive(ref ep);
                        if (sdfasd.Length / 16 < c)
                        {
                            throw new Exception();
                        }
                        for (var j = 0; c > j; ++j)
                        {
                            yield return(Unsafe.As <byte, Quadruple>(ref sdfasd[16 * j + 0]));
                        }
                        c = 0;
                    }
                }
                {
                    if (c > 0)
                    {
                        var        a      = nc.Send(sb, 64 * c, "localhost", 16383);
                        IPEndPoint ep     = null;
                        var        sdfasd = nc.Receive(ref ep);
                        if (sdfasd.Length / 16 < c)
                        {
                            throw new Exception();
                        }
                        for (var j = 0; c > j; ++j)
                        {
                            yield return(Unsafe.As <byte, Quadruple>(ref sdfasd[16 * j + 0]));
                        }
                        c = 0;
                    }
                }
            }
Пример #23
0
        public IEnumerable <Toot> Load(bool hasFederated, int take = int.MaxValue)
        {
            var accessors = new List <ITootAccessor>
            {
                new TootLocal()
            };

            if (hasFederated)
            {
                accessors.AddRange(_federates.Select(f => new TootRemote(f) as ITootAccessor));
            }
#if NET35
            var sync    = new object();
            var toots   = new List <Toot>();
            var counter = accessors.Count;
            var wait    = new System.Threading.AutoResetEvent(false);

            foreach (var accessor in accessors)
            {
                System.Threading.ThreadPool.QueueUserWorkItem(acs =>
                {
                    var loaded = (acs as ITootAccessor).Load(take);

                    System.Threading.Interlocked.Decrement(ref counter);
                    lock (sync)
                    {
                        if (loaded != null)
                        {
                            toots.AddRange(loaded);
                        }
                    }

                    if (counter <= 0)
                    {
                        wait.Set();
                    }
                }, accessor);
            }
            wait.WaitOne();
#else
            var toots = new System.Collections.Concurrent.ConcurrentBag <Toot>();
            var tasks = accessors.Select(acs => System.Threading.Tasks.Task.Factory.StartNew(() =>
            {
                var loaded = (acs as ITootAccessor).Load(take);
                if (loaded != null)
                {
                    foreach (var t in loaded)
                    {
                        toots.Add(t);
                    }
                }
            }));
            System.Threading.Tasks.Task.WaitAll(tasks.ToArray());
#endif
            return(toots.OrderByDescending(t => t.CreateAt).ThenBy(t => t.IsRemote));
        }
Пример #24
0
        //全ピクセルを詰め込んでから重複を取り除いたリストをカウントする
        //遅い、メモリを大量に消費する
        private int Count8Concurrent32bppとビットシフト(byte[] pixels)
        {
            var concurrent = new System.Collections.Concurrent.ConcurrentBag <uint>();

            Parallel.For(0, pixels.Length / 4, i =>
            {
                concurrent.Add((uint)(pixels[i * 4] | (pixels[i * 4 + 1] << 8) | (pixels[i * 4 + 2] << 16) | (pixels[i * 4 + 3] << 24)));
            });
            return(concurrent.Distinct().ToArray().Length);
        }
        public static List <SSCCListModel> ConvertSsccList(IEnumerable <API_SSCC_OVERVIEW_Result> input)
        {
            DateTime now = DateTime.Now;

            System.Collections.Concurrent.ConcurrentBag <SSCCListModel> slmList = new System.Collections.Concurrent.ConcurrentBag <SSCCListModel>();


            Parallel.ForEach(input, (item) =>
            {
                SSCCListModel slm = new SSCCListModel();
                slm.OrderDate     = item.FIRST_RECEIPT_DATE;
                slm.SSCC          = item.SSCC;
                slm.ActorFrom     = GetActorName(item.ACTOR_ORIGIN_ID);
                slm.ActorTo       = item.ACTOR_ID.HasValue ? GetActorName(item.ACTOR_ID.Value) : string.Empty;

                switch (item.SSCC_STATUS)
                {
                case 1:
                    slm.SsccStatus = "New";
                    break;

                case 2:
                    slm.SsccStatus = "Processed";
                    break;

                case 3:
                    slm.SsccStatus = "Validated";
                    break;

                case 4:
                    slm.SsccStatus = "Processing";
                    break;

                default:
                    slm.SsccStatus = "New";
                    break;
                }

                if (item.VALIDATION_DEADLINE.HasValue)
                {
                    slm.ValidationDeadline = Math.Round((item.VALIDATION_DEADLINE.Value - now).TotalHours, 0);
                }

                slm.SlaOK            = item.SLA_VALUE == item.SLA_MIN_VALUE;
                slm.CountingOK       = item.SHOP_COUNT == item.CI_COUNT;
                slm.CIDate           = item.CI_DATETIME;
                slm.IsValidated      = item.VALIDATED;
                slm.ValidationStatus = SetValidationStatus(item.VALIDATED, item.VALIDATION_DEADLINE);
                slm.ShipmentNumber   = item.SHIPMENT_NUMBER;
                slmList.Add(slm);
            });

            return(slmList.ToList());
        }
Пример #26
0
 /// <summary>
 /// 执行与释放或重置托管资源相关的应用程序定义的任务。
 /// </summary>
 protected override void DisposeManaged()
 {
     if (this._logger != null)
     {
         this._logger.Write(this._Items.ToArray());
     }
     this._logger = null;
     this._Items  = null;
     Log.ResetContext();
     base.DisposeManaged();
 }
        /// <summary>
        /// получение таблицы с абонентами
        /// </summary>
        /// <returns></returns>
        private IList <Meter> GetMeters()
        {
            int totalRows     = 0;
            int processedRows = 0;

            Model.WorkTask workTask = new("обработка картотеки");
            this.workTasksProgressViewModel.WorkTasks.Add(workTask);

            // таблица с абонентами
            totalRows = this.collectionKARTAB.Length;

            System.Collections.Concurrent.ConcurrentBag <Meter> metersList = new System.Collections.Concurrent.ConcurrentBag <Meter>();

            string s = string.Empty;

            workTask.UpdateStatus($"количество строк в таблице абонентов: {totalRows:N0}");
            processedRows = 0;
            workTask.StartProcessing();

            Parallel.ForEach(this.collectionKARTAB, (abonent) =>
            {
                ICollection <KARTSCH> abonentMeters = this.GetDictionaryValue(this.dictionaryKARTSCH, abonent.LIC_SCH);

                if (abonentMeters != null && abonentMeters.Count != 0)
                {
                    var rowsGroupedByMeter = abonentMeters
                                             .GroupBy(
                        i => i.N_SCH.ToString().Trim(),
                        (meterNumber, list) => new { MeterNumber = meterNumber, List = list.ToArray() });

                    foreach (var group in rowsGroupedByMeter)
                    {
                        KARTSCH meterDataRow = group.List[0];
                        Meter meter          = this.ParseDataRowAndGetMeter(meterDataRow, abonent);
                        meter.Тарифов        = group.List.Length > 1 ? (byte)group.List.Length : (byte)1;
                        metersList.Add(meter);
                    }
                }
                else
                {
                    Meter meter   = this.ParseDataRowAndGetMeter(null, abonent);
                    meter.Тарифов = 0;
                    metersList.Add(meter);
                }

                workTask.UpdateUI(++processedRows, totalRows);
            });

            // fix
            workTask.UpdateUI(totalRows, totalRows);

            return(this.SortData(metersList, workTask));
        }
Пример #28
0
 public ProgramVisualizer()
 {
     graph              = new Graph("P# Program");
     Machines           = new HashSet <string>();
     States             = new Dictionary <string, HashSet <string> >();
     MachineToSubgraph  = new Dictionary <string, Subgraph>();
     States             = new Dictionary <string, HashSet <string> >();
     StateToNode        = new Dictionary <Tuple <string, string>, Node>();
     CollapsedMachines  = new HashSet <string>();
     Transitions        = new Dictionary <Transition, Edge>();
     PendingTransitions = new System.Collections.Concurrent.ConcurrentBag <Transition>();
 }
Пример #29
0
        private static void ComputeTargets(System.Collections.Concurrent.ConcurrentBag <TargetOrderPair> bag, int i,
                                           List <Plane> planes, List <Speed> speeds, List <Zone> zones, List <Tool> tools, List <CSystem> wobjs,
                                           List <double> eRotVals, List <double> eLinVals, List <int> methods)
        {
            TargetOrderPair result = new TargetOrderPair();

            // Create the robot target.
            result.Value = new ABBTarget(planes[i], (MotionType)methods.InfinitElementAt(i), speeds.InfinitElementAt(i), zones.InfinitElementAt(i),
                                         tools.InfinitElementAt(i), wobjs.InfinitElementAt(i), eRotVals.InfinitElementAt(i), eLinVals.InfinitElementAt(i));
            result.Order = i;

            bag.Add(result);
        }
Пример #30
0
        private static void ComputeJointTargets(System.Collections.Concurrent.ConcurrentBag <TargetOrderPair> bag, int i,
                                                List <List <double> > angles, List <Speed> speeds, List <Zone> zones, List <Tool> tools,
                                                List <double> eRotVals, List <double> eLinVals)
        {
            TargetOrderPair result = new TargetOrderPair();

            // Create the robot target.
            result.Value = new ABBTarget(angles[i], speeds.InfinitElementAt(i), zones.InfinitElementAt(i),
                                         tools.InfinitElementAt(i), eRotVals.InfinitElementAt(i), eLinVals.InfinitElementAt(i));
            result.Order = i;

            bag.Add(result);
        }
Пример #31
0
        private IEnumerable <Person> ConvertInParallel(string[] contacts)
        {
            var bag = new System.Collections.Concurrent.ConcurrentBag <Person>();

            Parallel.ForEach(contacts, (contact) =>
            {
                if (TryConvert(contact, out Person person))
                {
                    bag.Add(person);
                }
            });
            return(bag.ToList());
        }
Пример #32
0
        /// <summary>
        /// Asynchronously converts all of the .pdf files it receives into .txt files
        /// If no arguments are supplied, it will instead convert all yet unconverted .pdf files in the project directory
        /// Results are stored in corresponding project directory
        /// </summary>
        /// <param name="files">0 or more instances of the PdfFile class which encapsulate .pdf files.</param>
        private static async Task <IEnumerable <TxtFile> > ConvertPdfToTextAsync(IEnumerable <PdfFile> files)
        {
            ThrowIfUninitialized();
            var convertedFiles = new System.Collections.Concurrent.ConcurrentBag <TxtFile>();

            foreach (var pdf in files.Except <InputFile>(taggedFiles))
            {
                var converted = await new PdfToTextConverter(pdf as PdfFile).ConvertFileAsync();
                convertedFiles.Add(converted);
                AddFile(converted.FullPath);
                File.Delete(converted.FullPath);
            }
            return(convertedFiles);
        }
 public static void RemoveTempFiles()
 {
     foreach (IDisposable disposable in TempFileHandlers)
     {
         try
         {
             disposable.Dispose();
         }
         catch (Exception e) {
             SharedUtils.Logger.Log("Exception when disposing Temp file: " + e.Message, SharedUtils.Logger.EventLogEntryType.Error);
         }
     }
     TempFileHandlers = new System.Collections.Concurrent.ConcurrentBag <IDisposable>();
 }
Пример #34
0
 static AliyunLogger()
 {
     if (null != System.Reflection.Assembly.GetEntryAssembly())
     {
         _appName = System.Reflection.Assembly.GetEntryAssembly().GetName().Name;
         _ver     = System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString();
     }
     else
     {
         _appName = "";
         _ver     = "0.0.0.0";
     }
     logs = new System.Collections.Concurrent.ConcurrentBag <LogInfo>();
 }
        public void TestForEach()
        {
            // Arrange
            var actor = new FieldForEachParallel();
            var fields = typeof(FieldForEachEntity).GetFields(BindingFlags.NonPublic | BindingFlags.Instance).OrderBy(fi => fi.Name);
            var entity = new FieldForEachEntity();
            var actual = new System.Collections.Concurrent.ConcurrentBag<string>();
            const int EXPECTED = 10;

            // Act
            actor.ForEach(entity, new QuillInjectionContext(), fields, (e, field, context) => actual.Add(field.Name));

            // Assert
            Assert.AreEqual(EXPECTED, actual.Count());
        }
Пример #36
0
        private bool _disposedValue; // To detect redundant calls
   
        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (_disposedValue) return;

            if (disposing)
            {
                if (_disposables != null)
                {
                    foreach (var obj in _disposables)
                    {
                        obj.Dispose();
                    }
                    _disposables = null;
                }
            }
            _disposedValue = true;
        }
Пример #37
0
        public void MultiGetTest()
        {
            Parallel.ForEach(clients, client =>
            {
                var keys = new System.Collections.Concurrent.ConcurrentBag<string>();
                Parallel.For(0, 1000, i =>
                {
                    var key = Guid.NewGuid().ToString();
                    Assert.IsTrue(client.Set(key, stringValue, TimeSpan.FromMinutes(10)));
                    keys.Add(key);
                });

                var value = client.GetMultiple(keys.ToList());
                Assert.AreEqual(value.Count, keys.Count);
                foreach (var item in value)
                {
                    Assert.AreEqual(item.Value, stringValue);
                }
            });
        }
Пример #38
0
        public override void Run(bool runChildren)
        {
            RawlerLib.Timer.StopWatch.Write("RawlerAutoNextLink urlListCreate");
            urlList = new System.Collections.Concurrent.ConcurrentBag<string>();
            RawlerLib.Timer.StopWatch.Write("RawlerAutoNextLink  autoNextLink.Run");

            autoNextLink.Run();
            RawlerLib.Timer.StopWatch.Write("RawlerAutoNextLink  autoNextLink.Run End");

            var page = this.GetUpperRawler<Page>();
            if (page != null)
            {
                if (maxCount > count)
                {
                    var url = page.GetCurrentUrl();
                    Uri url_uri = new Uri(url);
                    if (urlList.Any())
                    {
                        var test = urlList.Distinct().Where(n => new Uri(n).Host == url_uri.Host && urlHash.Contains(n) == false).ToList();
                        var nextUrl = urlList.Distinct().Where(n => new Uri(n).Host == url_uri.Host && urlHash.Contains(n) == false)
                            .Select(n => new { url = n, Distance = Rawler.NPL.LevenshteinDistance.Compute(url, n) })
                            .OrderBy(n => n.Distance);
                        if (nextUrl.Any())
                        {
                            page.PushUrl(nextUrl.First().url);
                            urlHash.Add(nextUrl.First().url);
                            count++;
                        }
                        urlHash.Add(url);

                    }
                    else
                    {
                        ReportManage.ErrReport(this, "NextLinkの取得がありません");
                    }
                }
            }
            RawlerLib.Timer.StopWatch.Write("RawlerAutoNextLink  End");

            base.Run(runChildren);
        }
Пример #39
0
        private static Tuple<double, double> GetSCGasMonteCarlo(MarginalGas gas, double prtp, bool equityWeights, int monteCarloRuns)
        {
            var parameters = new Parameters();
            parameters.ReadDirectory(@"Data\Base");

            var fm = FundModel.GetModel();
            fm.Run(parameters.GetBestGuess());

            var rand = new jp.takel.PseudoRandom.MersenneTwister();

            var sccs = new System.Collections.Concurrent.ConcurrentBag<double>();

            Parallel.ForEach(parameters.GetRandom(rand, monteCarloRuns), pv =>
            {
                var m = new MarginalDamage3()
                {
                    EmissionYear = Timestep.FromYear(2010),
                    Eta = 1.0,
                    Gas = gas,
                    Parameters = pv,
                    Prtp = prtp,
                    UseEquityWeights = equityWeights,
                    YearsToAggregate = 290
                };

                double scc = m.Start();

                sccs.Add(scc);
            });

            var stats = new DescriptiveStatistics(sccs);

            return Tuple.Create(stats.Mean, Math.Sqrt(stats.Variance) / Math.Sqrt(stats.Count));
        }
        /// <summary>
        ///   Performs object detection on the given frame.
        /// </summary>
        /// 
        //public Rectangle[] ProcessFrame(Bitmap frame)
        //{
        //    using (FastBitmap fastBitmap = new FastBitmap(frame))
        //    {
        //        return ProcessFrame(fastBitmap);
        //    }
        //}

        /// <summary>
        ///   Performs object detection on the given frame.
        /// </summary>
        /// 
        public Rectangle[] ProcessFrame(Bitmap image)
        {
          //  int colorChannel =
           //   image.PixelFormat == PixelFormat.Format8bppIndexed ? 0 : channel;

            Rectangle[] objects;

            // Creates an integral image representation of the frame
            using (FastBitmap fastBitmap = new FastBitmap(image, this.classifier.Cascade.HasTiltedFeatures))
            {
                // Creates a new list of detected objects.
                this.detectedObjects.Clear();

                int width = fastBitmap.Width;
                int height = fastBitmap.Height;

                // Update parameters only if different size
                if (steps == null || width != lastWidth || height != lastHeight)
                    update(width, height);


                Rectangle window = Rectangle.Empty;

                // For each scaling step
                for (int i = 0; i < steps.Length; i++)
                {
                    float scaling = steps[i];

                    // Set the classifier window scale
                    classifier.Scale = scaling;

                    // Get the scaled window size
                    window.Width = (int)(baseWidth * scaling);
                    window.Height = (int)(baseHeight * scaling);

                    // Check if the window is lesser than the minimum size
                    if (window.Width < minSize.Width || window.Height < minSize.Height)
                    {
                        // If we are searching in greater to smaller mode,
                        if (scalingMode == ObjectDetectorScalingMode.GreaterToSmaller)
                        {
                            break; // it won't get bigger, so we should stop.
                        }
                        else continue; // continue until it gets greater.
                    }

                    // Check if the window is greater than the maximum size
                    else if (window.Width > maxSize.Width || window.Height > maxSize.Height)
                    {
                        // If we are searching in greater to smaller mode,
                        if (scalingMode == ObjectDetectorScalingMode.GreaterToSmaller)
                        {
                            continue; // continue until it gets smaller.
                        }

                        break; // it won't get smaller, so we should stop. 
                    }

                    // Grab some scan loop parameters
                    int xStep = window.Width >> 3;
                    int yStep = window.Height >> 3;

                    int xEnd = width - window.Width;
                    int yEnd = height - window.Height;


                    // Parallel mode. Scan the integral image searching
                    // for objects in the window with parallelization.
                    var bag = new System.Collections.Concurrent.ConcurrentBag<Rectangle>();

                    int numSteps = (int)Math.Ceiling((double)yEnd / yStep);

                    // For each pixel in the window column
                    var window1 = window;
                    Parallel.For(
                        0,
                        numSteps,
                        (j, options) =>
                        {
                            int y = j * yStep;

                            // Create a local window reference
                            Rectangle localWindow = window1;

                            localWindow.Y = y;

                            // For each pixel in the window row
                            for (int x = 0; x < xEnd; x += xStep)
                            {
                                if (options.ShouldExitCurrentIteration) return;

                                localWindow.X = x;

                                // Try to detect and object inside the window
                                if (classifier.Compute(fastBitmap, localWindow))
                                {
                                    // an object has been detected
                                    bag.Add(localWindow);

                                    if (searchMode == ObjectDetectorSearchMode.Single)
                                        options.Stop();
                                }
                            }
                        });

                    // If required, avoid adding overlapping objects at
                    // the expense of extra computation. Otherwise, only
                    // add objects to the detected objects collection.
                    if (searchMode == ObjectDetectorSearchMode.NoOverlap)
                    {
                        foreach (Rectangle obj in bag)
                        {
                            if (!overlaps(obj))
                            {
                                detectedObjects.Add(obj);
                            }
                        }
                    }
                    else if (searchMode == ObjectDetectorSearchMode.Single)
                    {
                        if (bag.TryPeek(out window))
                        {
                            detectedObjects.Add(window);
                            break;
                        }
                    }
                    else
                    {
                        foreach (Rectangle obj in bag)
                        {
                            detectedObjects.Add(obj);
                        }
                    }
                }
            }

            objects = detectedObjects.ToArray();

            if (searchMode == ObjectDetectorSearchMode.Average)
            {
                objects = match.Group(objects);
            }

            checkSteadiness(objects);
            lastObjects = objects;

            return objects; // Returns the array of detected objects.
        }
Пример #41
0
        /// <summary>
        ///   Performs object detection on the given frame.
        /// </summary>
        /// 
        public Rectangle[] ProcessFrame(UnmanagedImage image)
        {
            int colorChannel =
              image.PixelFormat == PixelFormat.Format8bppIndexed ? 0 : channel;

            // Creates an integral image representation of the frame
            IntegralImage2 integralImage = IntegralImage2.FromBitmap(
                image, colorChannel, classifier.Cascade.HasTiltedFeatures);

            // Creates a new list of detected objects.
            this.detectedObjects.Clear();

            int width = integralImage.Width;
            int height = integralImage.Height;

            // Update parameters only if different size
            if (steps == null || width != lastWidth || height != lastHeight)
                update(width, height);


            Rectangle window = Rectangle.Empty;

            // For each scaling step
            for (int i = 0; i < steps.Length; i++)
            {
                float scaling = steps[i];

                // Set the classifier window scale
                classifier.Scale = scaling;

                // Get the scaled window size
                window.Width = (int)(baseWidth * scaling);
                window.Height = (int)(baseHeight * scaling);

                // Check if the window is lesser than the minimum size
                if (window.Width < minSize.Width || window.Height < minSize.Height)
                {
                    // If we are searching in greater to smaller mode,
                    if (scalingMode == ObjectDetectorScalingMode.GreaterToSmaller)
                    {
                        goto EXIT; // it won't get bigger, so we should stop.
                    }
                    else continue; // continue until it gets greater.
                }

                // Check if the window is greater than the maximum size
                else if (window.Width > maxSize.Width || window.Height > maxSize.Height)
                {
                    // If we are searching in greater to smaller mode,
                    if (scalingMode == ObjectDetectorScalingMode.GreaterToSmaller)
                    {
                        continue; // continue until it gets smaller.
                    }
                    else goto EXIT; // it won't get smaller, so we should stop.                    }
                }

                // Grab some scan loop parameters
                int xStep = window.Width >> 3;
                int yStep = window.Height >> 3;

                int xEnd = width - window.Width;
                int yEnd = height - window.Height;


                if (!parallel)  // Check if we should run in parallel
                {
                    // Sequential mode. Scan the integral image searching
                    // for objects in the window without parallelization.

                    // For every pixel in the window column
                    for (int y = 0; y < yEnd; y += yStep)
                    {
                        window.Y = y;

                        // For every pixel in the window row
                        for (int x = 0; x < xEnd; x += xStep)
                        {
                            window.X = x;

                            if (searchMode == ObjectDetectorSearchMode.NoOverlap && overlaps(window))
                                continue; // We have already detected something here, moving along.

                            // Try to detect an object inside the window
                            if (classifier.Compute(integralImage, window))
                            {
                                // object has been detected
                                detectedObjects.Add(window);

                                if (searchMode == ObjectDetectorSearchMode.Single)
                                    goto EXIT; // stop on first object found
                            }
                        }
                    }
                }

#if !NET35
                else // use parallel processing
                {
                    // Parallel mode. Scan the integral image searching
                    // for objects in the window with parallelization.
                    var bag = new System.Collections.Concurrent.ConcurrentBag<Rectangle>();

                    int numSteps = (int)Math.Ceiling((double)yEnd / yStep);

                    // For each pixel in the window column
                    Parallel.For(0, numSteps, (j, options) =>
                    {
                        int y = j * yStep;

                        // Create a local window reference
                        Rectangle localWindow = window;

                        localWindow.Y = y;

                        // For each pixel in the window row
                        for (int x = 0; x < xEnd; x += xStep)
                        {
                            if (options.ShouldExitCurrentIteration) return;

                            localWindow.X = x;

                            // Try to detect and object inside the window
                            if (classifier.Compute(integralImage, localWindow))
                            {
                                // an object has been detected
                                bag.Add(localWindow);

                                if (searchMode == ObjectDetectorSearchMode.Single)
                                    options.Stop();
                            }
                        }
                    });

                    // If required, avoid adding overlapping objects at
                    // the expense of extra computation. Otherwise, only
                    // add objects to the detected objects collection.
                    if (searchMode == ObjectDetectorSearchMode.NoOverlap)
                    {
                        foreach (Rectangle obj in bag)
                            if (!overlaps(obj)) detectedObjects.Add(obj);
                    }
                    else if (searchMode == ObjectDetectorSearchMode.Single)
                    {
                        if (bag.TryPeek(out window))
                        {
                            detectedObjects.Add(window);
                            goto EXIT;
                        }
                    }
                    else
                    {
                        foreach (Rectangle obj in bag)
                            detectedObjects.Add(obj);
                    }
                }
#endif
            }


        EXIT:

            Rectangle[] objects = detectedObjects.ToArray();

            if (searchMode == ObjectDetectorSearchMode.Average)
                objects = match.Group(objects);

            checkSteadiness(objects);
            lastObjects = objects;

            return objects; // Returns the array of detected objects.
        }
Пример #42
0
        public List<StreamItem> Execute(Subscription subscription)
        {
            _Subscription = subscription;

            var bag = new System.Collections.Concurrent.ConcurrentBag<StreamItem>();

            try
            {
                if (Feed != null)
                {
                    XNamespace ns = "http://www.w3.org/2005/Atom";

                    // RSS
                    if (Feed.Element("channel") != null)
                    {
                        var items = (from item in Feed.Element("channel").Elements("item").AsParallel() select item).ToList();
                        Parallel.ForEach(items, i =>
                        {
                            var streamItem = new StreamItem();
                            streamItem.Timestamp = Utilities.Rfc822DateTime.FromString(i.Element("pubDate").Value);
                            streamItem.Url = i.Element("link").Value;
                            streamItem.Title = i.Element("title").Value;
                            streamItem.Icon = _Subscription.Icon;

                            if (i.Element("description") != null)
                                streamItem.Description = i.Element("description").Value;

                            bag.Add(streamItem);
                        });
                    }

                    // Atom
                    else if (Feed.Element(ns + "entry") != null)
                    {
                        var items = (from item in Feed.Elements(ns + "entry").AsParallel() select item).ToList();
                        Parallel.ForEach(items, i =>
                        {
                            var streamItem = new StreamItem();
                            streamItem.Timestamp = Utilities.Rfc822DateTime.FromString(i.Element(ns + "published").Value);
                            streamItem.Url = i.Elements(ns + "link").Single(e => (e.Attribute("rel").Value == "alternate")).Attribute("href").Value;
                            streamItem.Title = i.Element(ns + "title").Value;
                            streamItem.Icon = _Subscription.Icon;

                            if (i.Element(ns + "content") != null)
                                streamItem.Description = i.Element(ns + "content").Value;

                            bag.Add(streamItem);
                        });
                    }

                    // Invalid
                    else
                        bag = null;

                }
            }
            catch (Exception e)
            {
                // We got upset by the RSS/Atom, load previous items.
                // TODO: list = bla bla.
            }
            finally
            {
                if (bag != null && bag.Count > 0)
                {
                    Parallel.ForEach(bag, i =>
                    {
                        if (i != null && !string.IsNullOrEmpty(i.Description))
                        {
                            var img = Regex.Match(i.Description, "src=[\"]?([^\" >]+)", RegexOptions.Compiled);
                            if (img.Success)
                                i.Description = "<img " + img.Value + "\" class=\"embedded-image\">";
                            else
                                i.Description = string.Empty;
                        }
                    });
                }
            }

            return bag != null && bag.Count > 0 ? bag.ToList() : null;
        }
Пример #43
0
        /// <summary>
        /// Select the next vertex from the queue.
        /// </summary>
        /// <returns></returns>
        private uint? SelectNext()
        {
            // first check the first of the current queue.
            while (_queue.Count > 0)
            { // get the first vertex and check.
                // TODO: peek and get the weight at the same time.
                uint first_queued = _queue.Peek();

                // the lazy updating part!
                // calculate priority
                float priority = _calculator.Calculate(first_queued);
                float current_priority = _queue.Weight(first_queued);
                if (priority != current_priority)
                { // a succesfull update.
                    _misses_queue.Enqueue(true);
                    _misses++;
                }
                else
                { // an unsuccessfull update.
                    _misses_queue.Enqueue(false);
                }
                if (_misses_queue.Count > _k)
                { // dequeue and update the misses.
                    if (_misses_queue.Dequeue())
                    {
                        _misses--;
                    }
                }

                // if the misses are _k
                if (_misses == _k)
                { // recalculation.
                    CHPriorityQueue new_queue = new CHPriorityQueue();
            #if !WINDOWS_PHONE
                    System.Collections.Concurrent.ConcurrentBag<KeyValuePair<uint, float>> recalculated_weights =
                        new System.Collections.Concurrent.ConcurrentBag<KeyValuePair<uint, float>>();
                    System.Threading.Tasks.Parallel.ForEach(_queue, vertex =>
                    {
                        recalculated_weights.Add(
                            new KeyValuePair<uint,float>(vertex, _calculator.Calculate(vertex)));
                    });
            #endif
            #if WINDOWS_PHONE
                    HashSet<KeyValuePair<uint, float>> recalculated_weights =
                        new HashSet<KeyValuePair<uint, float>>();
                    foreach (uint vertex in _queue)
                    {
                        recalculated_weights.Add(
                            new KeyValuePair<uint, float>(vertex, _calculator.Calculate(vertex)));
                    }
            #endif
                    foreach (KeyValuePair<uint, float> pair in recalculated_weights)
                    {
                        new_queue.Enqueue(pair.Key, pair.Value);
                    }
                    _queue = new_queue;
                    _misses_queue.Clear();
                    _misses = 0;
                }
                else
                { // no recalculation.
                    if (priority > current_priority)
                    { // re-enqueue the weight.
                        _queue.Enqueue(first_queued, priority);
                    }
                    else
                    {
                        //if (this.CanBeContracted(first_queued))
                        //{ // yet, this vertex can be contracted!
                        _queue.Remove(first_queued);
                        return first_queued;
                    }
                }
            }

            //// keep going over the enumerator and check.
            //while (_all_nodes.MoveNext())
            //{
            //    // get the next vertex.
            //    uint next = _all_nodes.Current;

            //    if (this.CanBeContracted(next))
            //    { // yes, this vertex can be contracted!
            //        _queue.Remove(next);
            //        return next;
            //    }
            //}

            // check the queue.
            if (_queue.Count > 0)
            {
                throw new Exception("Unqueued items left!, CanBeContracted is too restrictive!");
            }
            return null; // all nodes have been contracted.
        }
Пример #44
0
 public ProgramVisualizer()
 {
     graph = new Graph("P# Program");
     Machines = new HashSet<string>();
     States = new Dictionary<string, HashSet<string>>();
     MachineToSubgraph = new Dictionary<string, Subgraph>();
     States = new Dictionary<string, HashSet<string>>();
     StateToNode = new Dictionary<Tuple<string, string>, Node>();
     CollapsedMachines = new HashSet<string>();
     Transitions = new Dictionary<Transition, Edge>();
     PendingTransitions = new System.Collections.Concurrent.ConcurrentBag<Transition>();
 }
Пример #45
0
        static void Main(string[] args)
        {
            Adhesive.Common.AdhesiveFramework.Start();

            var client = MemcachedClient.GetClient("TestMemcachedCluster");

            //client.Flush();

            //var stat = client.Stat();


            var lockerkey = Guid.NewGuid().ToString();
            client.AcquireLock(lockerkey, TimeSpan.FromSeconds(1));
            Parallel.For(0, 10, i =>
            {
                try
                {
                    using (var clocker = client.AcquireLock(lockerkey, TimeSpan.FromSeconds(1)))
                    {
                        Console.WriteLine(i + "\t" + DateTime.Now.ToString("HH:mm:ss fff") + " 获得锁");
                        Thread.Sleep(1000);
                    }
                }
                catch (TimeoutException ex)
                {
                    Console.WriteLine(i + "\t" + DateTime.Now.ToString("HH:mm:ss fff") + " " + ex.Message);
                }
            });


            Console.ReadLine();
            //故障测试

            //Console.ReadLine();
            while (true)
            {
                Console.Clear();
                Parallel.For(0, 10, i =>
                {
                    LocalLoggingService.Debug("Get Key:" + "failtest" + i);
                    var v = client.Get("failtest" + i);
                    if (v == null)
                    {
                        LocalLoggingService.Debug("没有取到 Key:" + "failtest" + i);
                        bool b = client.Set("failtest" + i, "阿斯达撒大的" + i);
                        LocalLoggingService.Debug("Set Key:" + "failtest" + i + ":" + b.ToString());
                    }
                    else
                    {
                        LocalLoggingService.Debug("取到 Key:" + "failtest" + i + "  Value:" + v);
                    }
                });
                Thread.Sleep(1000);
            }

            //性能测试

            while (true)
            {
                var pcount = 100000;
                Stopwatch sw = Stopwatch.StartNew();
                var p = Guid.NewGuid().ToString().Substring(0, 10);
                Parallel.For(0, pcount, i =>
                {
                    if (!client.Set(p + i, new f**k
                    {
                        name = "你爷爷",
                        times = i,
                    }, TimeSpan.FromMinutes(5)))
                        LocalLoggingService.Warning("Set错误" + i);
                });
                LocalLoggingService.Info("写测试 次数:{0} 时间:{1} 每秒:{2}", pcount, sw.ElapsedMilliseconds, pcount * 1000 / sw.ElapsedMilliseconds);
                sw.Restart();
                Parallel.For(0, pcount, i =>
                {
                    client.FastSet(p + i, new f**k
                    {
                        name = "你爷爷",
                        times = i,
                    }, TimeSpan.FromMinutes(5));
                });
                LocalLoggingService.Info("快速写测试 次数:{0} 时间:{1} 每秒:{2}", pcount, sw.ElapsedMilliseconds, pcount * 1000 / sw.ElapsedMilliseconds);
                sw.Restart();
                Parallel.For(0, pcount, i =>
               {
                   if (client.Get<f**k>(p + i) == null)
                       LocalLoggingService.Warning("Get错误" + i);
               });
                LocalLoggingService.Info("读测试 次数:{0} 时间:{1} 每秒:{2}", pcount, sw.ElapsedMilliseconds, pcount * 1000 / sw.ElapsedMilliseconds);


            }
            Console.ReadKey();


            //测试List

            var listkey = Guid.NewGuid().ToString();
            int failed = 0;
            Parallel.For(0, 1000, i =>
            {
                var itemkey = Guid.NewGuid().ToString();
                if (!client.SetListItem(listkey, itemkey, new f**k
                {
                    name = "你爷爷",
                    times = i,
                }, TimeSpan.FromHours(1)))
                    Interlocked.Increment(ref failed);
            });

            //client.DeleteListItem("fucklist", "fuck5");

            List<f**k> listData = client.GetList<f**k>(listkey);
            Console.WriteLine(listData.Count);
            Console.WriteLine(failed);

            Console.ReadKey();

            // 为测试批量获取做准备
            var keys = new System.Collections.Concurrent.ConcurrentBag<string>();

            Parallel.For(0, 20, i =>
            {
                var t = i;
                var key = Guid.NewGuid().ToString();
                keys.Add(key);
                var f**k = new f**k
                {
                    name = "你妈",
                    times = t,
                };

                if (!client.Set(key, f**k, TimeSpan.FromSeconds(t + 1)))
                    throw new Exception("错误");
            });

            //为测试append做准备
            Console.WriteLine("append " + client.Set("append", "1"));
            Console.WriteLine("prepend " + client.Set("prepend", "1"));
            Console.WriteLine("replace " + client.Replace("replace", "test"));

            Console.WriteLine(client.Add("add", "test"));
            Console.WriteLine("add again " + client.Add("add", "test"));

            //测试cas
            var caskey = "castest";
            ulong version = 0;
            client.Set(caskey, caskey, version);
            client.Get(caskey, out version);
            Console.WriteLine(client.Set(caskey, caskey, version));
            Console.WriteLine(client.Set(caskey, caskey, version));

            Console.WriteLine("press key");
            Console.ReadKey();

            while (true)
            {
                Thread.Sleep(10);
                Console.Clear();
                var data = client.GetMultiple<f**k>(keys.ToList());
                foreach (var item in data)
                {
                    Console.WriteLine(item.Key + " " + item.Value);
                }

                Console.WriteLine("incr:" + client.IncrementWithInit("num1", 10, TimeSpan.FromSeconds(5), 5));
                Console.WriteLine("decr:" + client.DecrementWithInit("num2", 20, TimeSpan.FromSeconds(5), 5));

                client.Append("append", "2");
                Console.WriteLine("append:" + client.Get("append"));

                client.Prepend("prepend", "2");
                Console.WriteLine("prepend:" + client.Get("prepend"));

                var total = 1000;
                Parallel.For(0, total, new ParallelOptions { MaxDegreeOfParallelism = 4, }, i =>
                {
                    int t = i;
                    var key = "yzhu" + t;
                    var f**k = new f**k
                    {
                        name = new string('a', 1024),
                        times = t,
                    };

                    if (!client.Set(key, f**k))
                        throw new Exception("Set出错!" + key);
                });

                int count = 0;
                Parallel.For(0, total, new ParallelOptions { MaxDegreeOfParallelism = 4, }, i =>
                {
                    int t = i;
                    var key = "yzhu" + t;
                    if (client.Get<f**k>(key) != null)
                        Interlocked.Increment(ref count);
                    else
                        throw new Exception("Get出错!" + key);
                });

                if (count != total)
                    throw new Exception("没有取到所有数据" + count);
            }
        }
Пример #46
0
        /// <summary>
        /// 执行与释放或重置托管资源相关的应用程序定义的任务。
        /// </summary>
        protected override void DisposeManaged()
        {
            if(this._logger != null) this._logger.Write(this._Items.ToArray());

            this._logger = null;
            this._Items = null;
            Log.ClearContext();
            base.DisposeManaged();
        }
Пример #47
0
 public void ExpireTest()
 {
     Parallel.ForEach(clients, client =>
     {
         var expireSpan = TimeSpan.FromSeconds(1);
         var keys = new System.Collections.Concurrent.ConcurrentBag<string>();
         Parallel.For(0, Count, i =>
         {
             var key = Guid.NewGuid().ToString();
             Assert.IsTrue(client.Set(key, stringValue, expireSpan));
             keys.Add(key);
         });
         Thread.Sleep(expireSpan);
         Parallel.ForEach(keys, key =>
         {
             Assert.IsNull(client.Get(key));
         });
     });
 }
Пример #48
0
        public IEnumerable<CompareAnalyzeData> CompareData(string research_id)
        {
            var research = db.GetResearch(research_id);

            if (research == null) return new List<CompareAnalyzeData>();
            var items = db.Items.Where(n => n.Resarch.ResearchId == research.ResearchId).Select(n => n.ItemId).ToArray();
            var itemAttribute = db.ItemAttributes.Where(n => items.Contains(n.Item.ItemId)).ToArray();
            var users = db.UserAnswerCompleted.Where(n => n.Research.ResearchId == research.ResearchId).Select(n => n.User).ToArray();
            var resultAll = from answer in db.ItemCompareAnsweres.Where(n => n.Research.ResearchId == research.ResearchId)
                            join complete in db.UserAnswerCompleted.Where(n => n.Research.ResearchId == research.ResearchId)
                            on answer.User.UserId equals complete.User.UserId into z
                            select answer;
            var groupUser = resultAll.GroupBy(n => n.User.UserId);
            var count = db.UserAnswerCompleted.Where(n => n.Research.ResearchId == research.ResearchId).Count();
            Dictionary<int, List<Tuple<string, string, double>>> userData = new Dictionary<int, List<Tuple<string, string, double>>>();

            foreach (var data in groupUser)
            {
                List<Tuple<string, string>> list = new List<Tuple<string, string>>();

                //属性の大小関係書き出し
                foreach (var item in data)
                {
                    if (item.ItemBad != null && item.ItemGood != null)
                    {
                        if (item.ItemBad.ItemAttribute != null && item.ItemGood.ItemAttribute != null)
                        {
                            var atribute = from good in item.ItemGood.ItemAttribute
                                           from bad in item.ItemBad.ItemAttribute
                                           where good.AttributeName == bad.AttributeName && good.Value != bad.Value
                                           select new { good = good.Value, bad = bad.Value };
                            foreach (var pair in atribute)
                            {
                                list.Add(new Tuple<string, string>(pair.good, pair.bad));
                            }
                        }
                    }
                }
                //ペアの組み合わせの数をかぞえる。
                var result = list.GroupBy(n => new { n.Item1, n.Item2 }).Select(n => new Tuple<string, string, int, string>(n.Key.Item1, n.Key.Item2, n.Count(), Tool.GetSortText(n.Key.Item1, n.Key.Item2))).GroupBy(n => n.Item4);

                List<Tuple<string, string, double>> list2 = new List<Tuple<string, string, double>>();
                foreach (var item in result)
                {
                    var tmp = item.OrderByDescending(n => n.Item3).ToArray();
                    if (tmp.Length > 1)
                    {
                        list2.Add(new Tuple<string, string, double>(tmp.First().Item1, tmp.First().Item2, (double)tmp.First().Item3 / (double)(tmp.First().Item3 + tmp.Last().Item3)));
                    }
                    else
                    {
                        list2.Add(new Tuple<string, string, double>(tmp.First().Item1, tmp.First().Item2, (double)tmp.First().Item3 / (double)(tmp.First().Item3)));
                    }
                }
                if (list2.Count > 0)
                {
                    userData.Add(data.Key, list2);
                }
            }

            System.Collections.Concurrent.ConcurrentBag<Tuple<string, List<string>, int>> stockData = new System.Collections.Concurrent.ConcurrentBag<Tuple<string, List<string>, int>>();

            System.Threading.Tasks.Parallel.ForEach(userData, (data) =>
                {
                    foreach (var item in Tool.ConverMuitiRelation(data.Value, 0.7, false))
                    {
                        stockData.Add(new Tuple<string, List<string>, int>(item.Item1.Aggregate((n, m) => n + "\t" + m), item.Item1, data.Key));
                    }
                });

            var result3 = stockData.GroupBy(n => n.Item1).Select(n => new CompareAnalyzeData()
               {
               AttributeOrder = n.First().Item2,
               Rate = (double)n.Count() * 100 / (double)userData.Count,
               Count = n.Count(),
               UserIds = n.Select(m => m.Item3).ToList()
               }).ToList();

            int i = 0;
            foreach (var item in result3)
            {
                item.Id = "compare" + i.ToString();
                List<int> idList = new List<int>(item.UserIds);
                item.QuestionAnswer = db.QuestionAnsweres.Where(n => idList.Contains(n.User.UserId)).GroupBy(n => n.QuestionChoice).Select(n => new { n.Key, Count = n.Count() }).ToArray().Select(n => new Tuple<QuestionChoice, int>(n.Key, n.Count));
                i++;
            }
            return result3;
        }
Пример #49
0
        public void Test(ITestContext context)
        {
            var failedChecks = new System.Collections.Concurrent.ConcurrentBag<SqlDeployResult>();
            var databasesCount = this.sqlDeploy.Settings.Databases.Count();
            var processedCount = 0;
            var databases = this.sqlDeploy.Settings.Databases.ToList();
            context.UpdateProgress(0, processedCount, databasesCount);
            Parallel.ForEach(
                databases,
                database =>
                {
                    var csBuilder = new SqlConnectionStringBuilder(database.ConnectionString);
                    var dbName = csBuilder.InitialCatalog;
                    var upToDate = database.Status.IsUpToDate;
                    try
                    {
                        var reconcileResult = this.sqlDeploy.ReconcileDatabase(database.DatabaseName);
                        if (upToDate && reconcileResult.IsSuccessful)
                        {
                            context.WriteLine(EventType.Success, Titles.DbIsUpToDate, dbName);
                        }
                        else
                        {
                            if (!upToDate)
                            {
                                context.WriteLine(
                                    EventType.Error,
                                    Titles.DbCheckFailed,
                                    dbName,
                                    Titles.SchemaNotUpToDate);
                                var result = new SqlDeployResult
                                                 {
                                                     IsSuccessful = false,
                                                     DatabaseName = dbName,
                                                     Exceptions =
                                                         new List<Exception>
                                                             {
                                                                 new Exception(
                                                                     Titles
                                                                     .SchemaNotUpToDate)
                                                             }
                                                 };
                                failedChecks.Add(result);
                            }
                            else
                            {
                                context.WriteLine(EventType.Error, Titles.DbCheckFailed, dbName, Titles.SchemaChanged);
                                failedChecks.Add(reconcileResult);
                            }
                        }
                    }
                    catch (SqlException ex)
                    {
                        context.WriteLine(
                                    EventType.Error,
                                    Titles.DbCheckFailed,
                                    dbName,
                                    ex.Message);
                                var result = new SqlDeployResult
                                                 {
                                                     IsSuccessful = false,
                                                     DatabaseName = dbName,
                                                     Exceptions =
                                                         new List<Exception>
                                                             {
                                                                 new Exception(ex.Message)
                                                             }
                                                 };
                                failedChecks.Add(result);
                    }

                    processedCount++;
                    context.UpdateProgress(0, processedCount, databasesCount);
                });
            
            if (failedChecks.Any())
            {
                var msg = string.Format(Titles.DbsNotUpToDate, string.Join(", ", failedChecks.Select(x => x.DatabaseName).ToArray()));
                Assert.Fails(msg);
            }
        }
Пример #50
0
        static void Main(string[] args)
        {
            int i = 0;
            var listHotels = new System.Collections.Concurrent.ConcurrentBag<Data>();
            /* BAliurl  * */
            var extUrl = "/RestaurantSearch?ajax=0&geo=294226&Action=PAGE&o=a{0}&etags=9910%2C9911%2C9909%2C9901%2C9899%2C9900";

              //  var extUrl = "/RestaurantSearch?ajax=0&geo=294265&Action=PAGE&o=a{0}&etags=9909%2C9899%2C9901%2C9900%2C9910%2C9911";
            var nods = Enumerable.Range(1, 7110).Where(p => p % 30 == 0);
            //var nods = Enumerable.Range(1, 40).Where(p => p % 30 == 0);
            Parallel.ForEach(nods, o =>
               // {

               //});
               //for (var o = 0; o <= 3480; )
               {
               try
               {

                   HtmlDocument doc = null;
                   for (var ic = 0; ic < 10 && doc == null; ic++)
                   {
                       // HtmlWeb web = new HtmlWeb();
                       // web.PreRequest += new HtmlWeb.PreRequestHandler(onPrereq);
                       try
                       {
                           doc = GetDoc(base_url + string.Format(extUrl, o));

                       }
                       catch (Exception e)
                       {
                           Console.WriteLine("error to load " + base_url + string.Format(extUrl, o) + " " + ic);
                           System.Threading.Thread.Sleep(1000);
                       }
                   }
                   //var nodes = doc.DocumentNode.Descendants().Where(p =>x p.Name == "div").Where(o => o.Id.Contains("hotel_")).Where(i => i.Attributes.FirstOrDefault(n => n.Value == "listing_title") != null);//.Where(p => p.Attributes.Contains("class='listing_info popIndexValidation'"));
                   if (doc != null)
                   {
                       var nodes = doc.DocumentNode.Descendants().Where(p => p.Name == "h3" && p.Attributes.FirstOrDefault(n => n.Value == "title") != null);//.Where(p => p.Attributes.Contains("class='listing_info popIndexValidation'"));
                       //  Parallel.ForEach(nodes, item =>
                       foreach (var item in nodes)
                       {
                           var test = item.ChildNodes.Where(p => p.NodeType == HtmlNodeType.Element && p.Name == "a").FirstOrDefault();
                           if (test != null)
                           {
                               var d = new Data();

                               d.Name = test.InnerText;

                               var url = test.Attributes.FirstOrDefault(p => p.Name == "href");
                               if (url != null)
                               {
                                   d.ID = ++i;
                                   d.url = url.Value;

                               }
                               d.ListUrl = base_url + d.url;// "/Restaurants-g294226-Bali.html";
                               listHotels.Add(d);
                               LoadReview(d);
                               WriteOutput(d);
                               Console.WriteLine(d.ToString());
                           }
                       }
                       //);
                   }
               }
               catch { }
               // j += 30;
               }
            );
            WriteOutput(listHotels.ToList());
            // HtmlNodeCollection tags = doc.DocumentNode.SelectNodes("//abc//tag");
        }
Пример #51
0
        public List<StreamItem> GetStreamItems()
        {
            var list = new System.Collections.Concurrent.ConcurrentBag<StreamItem>();
            var subscriptions = GetAllSubscriptions();

            Parallel.ForEach(subscriptions, s =>
            {
                var plugin = GetPluginFromSubscription(s);
                var items = new List<StreamItem>();
                var cachedItems = Context.Cache[s.ID.ToString()];

                items = cachedItems != null ? (List<StreamItem>)cachedItems : plugin.Execute(s);

                if (items != null && items.Count > 0)
                {
                    Context.Cache.Add(s.ID.ToString(), items.ToList(), null,
                        DateTime.Now.AddSeconds(CurrentSiteInfo.CacheDuration),
                        System.Web.Caching.Cache.NoSlidingExpiration,
                        System.Web.Caching.CacheItemPriority.Normal, null);

                    items.ForEach(item => list.Add(item));
                }
            });

            return list.Where(i => i != null && i.Timestamp != null)
                .OrderByDescending(i => i.Timestamp).ToList();
        }