public ActionResult Index(FormCollection formCollection)
        {
            var dossierId = Convert.ToInt32(formCollection["dossierId"]);

            var fields = fService.GetFieldsByDossier(dossierId);
            var list = new List<FieldInputz>();
            foreach (var field in fields)
            {
                var f = new FieldInputz();
                f.InjectFrom(field);
                decimal v;
                if (!decimal.TryParse(formCollection["c" + f.Id], out v))
                    f.ErrorMessage = "valoarea " + formCollection["c" + f.Id] + " nu este valida pentru acest camp";
                f.Value = v;
                list.Add(f);
            }

            if (list.Any(o => o.HasError))
            {
                ViewData["dossierId"] = dossierId;
                return View(list);
            }

            var fieldValues = list.AsParallel().Select(
                fieldInputz => new FieldValue { FieldId = fieldInputz.Id, Value = fieldInputz.Value, DossierId = dossierId }).ToList();

            dService.Init(fieldValues, dossierId);
            return View("success");
        }
Example #2
0
File: GHB.cs Project: Rarve/FRES
        protected List<string> GetUrlsFromPage(string pageUrl)
        {
            var urls = new List<string>();
            try
            {
                var html = Client.RetrieveHtmlStrGet(pageUrl).Result;
                urls = RegexHelper.GetMatchStr(html, @"(\/detail-)[0-9]{0,20}").Distinct().Select(x => URL_DTLS + x).ToList();

                var res = urls.AsParallel().WithDegreeOfParallelism(ParallismDegree).Select(x =>
                    new RealEstateE()
                    {
                        Url = x.Trim(),
                        State = 0,
                        RecordStatus = 1,
                        Source = SourceName
                    }
                ).ToList();

                DataHelper.InsertRealEstateE(res);
            }
            catch (Exception ex)
            {
                lock (sync)
                {
                    File.AppendAllText("D:/RE/A_" + this.GetType().Name + ".log", DateTime.Now.ToString("yyyyMMdd HH:mm") + "," + pageUrl + "," + ex.GetBaseException().Message + "\r\n");
                }
            }
            return urls;
        }
    private static List <KeyValuePair <int, int> > ConcurrentDictionary2(List <int> integers)
    {
        ConcurrentDictionary <int, int> result = new ConcurrentDictionary <int, int>();

        integers?.AsParallel().ForAll(x => { result.AddOrUpdate(x, 1, (key, old) => old + 1); });
        return(result.OrderByDescending(x => x.Value).ToList());
    }
Example #4
0
        /// <summary>
        /// To do. Create a clever search algorithm.
        /// </summary>
        /// <param name="Word"></param>
        /// <param name="Collection"></param>
        /// <returns></returns>
        public string GetSimilar(string Word, List<string> Collection)
        {
            if (Word.IsNullOrEmpty())
            {
                return Word;
            }

            Dictionary<string, int> keys = new Dictionary<string, int>();

            /*foreach (string s in words.Keys)
            {
                if (s.EndsWith(Word))
                {
                    keys.Add(s, s.Length);
                }
            }*/

            Collection.AsParallel().ForAll(s =>
            {
                if (s.EndsWith(Word))
                {
                    keys.Add(s, s.Length);
                }
            });

            if (!keys.Any() && Word.Length > 2)
            {
                return this.GetSimilar(Word.Substring(1), Collection);
            }

            string key = keys.OrderBy(val => val.Value).FirstOrDefault().Key;

            return key;
        }
Example #5
0
        public static void H()
        {
            Console.WriteLine("loading..");

            //init some stuff
            List<string> list = new List<string>();
            //  Stopwatch sw = new Stopwatch();
            Random r = new Random();
            double sum = 0;
            DateTime dt;
            for (int i = 0; i < 1000000; i++)
            {
                list.Add(r.Next(0, 999999).ToString());
            }

            //non parallel time
            Console.WriteLine("starting");

            dt = DateTime.Now;
            foreach (string f in list)
            {
                MD5(f);
            }
            Console.WriteLine($"non parallel time: {(DateTime.Now - dt).Seconds} seconds");

            //parallel
            sum = 0;
            dt = DateTime.Now;

               list.AsParallel().ForAll(str => MD5(str));

            Console.WriteLine($"parallel time:{ (DateTime.Now - dt).Seconds} seconds");

            Console.WriteLine("end");
        }
Example #6
0
File: Main.cs Project: danisein/Wox
        public static void IndexPrograms()
        {
            // todo why there is a lock??
            lock (lockObject)
            {
                var sources = DefaultProgramSources();
                if (_settings.ProgramSources != null &&
                    _settings.ProgramSources.Count(o => o.Enabled) > 0)
                {
                    sources.AddRange(_settings.ProgramSources);
                }

                _sources = sources.AsParallel()
                                  .Where(s => s.Enabled && SourceTypes.ContainsKey(s.Type))
                                  .Select(s =>
                                  {
                                      var sourceClass = SourceTypes[s.Type];
                                      var constructorInfo = sourceClass.GetConstructor(new[] { typeof(ProgramSource) });
                                      var programSource = constructorInfo?.Invoke(new object[] { s }) as IProgramSource;
                                      return programSource;
                                  })
                                  .Where(s => s != null).ToList();

                _programs = _sources.AsParallel()
                                    .SelectMany(s => s.LoadPrograms())
                                    // filter duplicate program
                                    .GroupBy(x => new { ExecutePath = x.Path, ExecuteName = x.ExecutableName })
                                    .Select(g => g.First())
                                    .ToList();

                _cache.Programs = _programs;
            }
        }
Example #7
0
        private void MoveFood(object sender, ElapsedEventArgs e)
        {
            this.Position = this.Position + this._direction * (40 - this._salts);

            List<Virus> clients = new List<Client>(this.Realm.Clients).OfType<Virus>().ToList();

            // Verifica se algum virus vai comer
            Virus virus = clients.AsParallel().FirstOrDefault(v => v.InsideClient(this.Position));
            if (virus != null)
            {
                virus.Score += this.Score;
                this.StopMove();
                this.Realm.RemoveFood(this);

                if (virus.Score > 200)
                    virus.Split(this._direction);

                if (this._time != null)
                    this._time.Stop();
            }
            else
            {
                if (this._salts++ > 10)
                    this._time.Stop();
            }
        }
Example #8
0
    static void Main(string[] args)
    {
        List<Student> list = new List<Student>()
        {
            new Student(){ ID=1, Name="jack", Age=20},
            new Student(){ ID=1, Name="mary", Age=25},
            new Student(){ ID=1, Name="joe", Age=29},
            new Student(){ ID=1, Name="Aaron", Age=25},
        };

        //这里我们会对age建立一组键值对
        var map = list.AsParallel().ToLookup(i => i.Age, count => 1);

        //化简统计
        var reduce = from IGrouping<int, int> singleMap
                     in map.AsParallel()
                     select new
                     {
                         Age = singleMap.Key,
                         Count = singleMap.Count()
                     };

        ///最后遍历
        reduce.ForAll(i =>
        {
            Console.WriteLine("当前Age={0}的人数有:{1}人", i.Age, i.Count);
        });
        Console.Read();
    }
Example #9
0
        /// <summary>
        /// Выполняет поиск 
        /// </summary>
        /// <param name="article"></param>
        /// <returns></returns>
        public IList<SearchResultItem> Search(string article)
        {
            // Список всех поставщиков
            var vendors = new List<IVendorSearcher>();

            // Формируем список поставщиков исходя из настроек
            var settingsRep = Locator.GetService<ISettingsRepository>();
            if (settingsRep.GetValue<bool>("search_autotrader"))
            {
                vendors.Add(new AutoTradeSearcher());
            }
            if (settingsRep.GetValue<bool>("search_mxgroup"))
            {
                vendors.Add(new MXGroupSearcher());
            }
            if (settingsRep.GetValue<bool>("search_berg"))
            {
                vendors.Add(new BergSearcher());
            }

            // Выбираем данные параллельно
            var fetched = vendors.AsParallel().Select(v => v.Search(article));

            // Формируем результат
            var result = new List<SearchResultItem>();
            foreach (var fetch in fetched)
            {
                result.AddRange(fetch.Where(r => r.Quantity != 0));
            }
            return result;
        }
 private static List <NumberCount> AsParallel2(List <int> integers)
 {
     return(integers?.AsParallel()
            .GroupBy(number => number)
            .Select(k => new NumberCount(k.Key, k.Count())) //Grap result, before sort
            .OrderByDescending(x => x.Occurrences)          //sort after result
            .ToList());
 }
 public static List<string> ParallelEncrypt(
     List<string> data,
     CancellationToken cancellationToken)
 {
     return data.AsParallel().WithCancellation(
         cancellationToken).Select(
             (item) => Encrypt(item)).ToList();
 }
 public IHttpActionResult Periodical(List<LabourPeriodical> periodicals)
 {
     periodicals.AsParallel().ForAll(x=>SmallQuoteEstimator.Australia.LabourEstimator.Periodical(x));
     return Ok(new
     {
         data = periodicals
     });
 }
Example #13
0
        public string Crack(List<string> plaintext, string target)
        {
            var targetBytes = HexHelper.StringToByteArray(target);
            ThreadLocal<MD5> md5 = new ThreadLocal<MD5>(() => MD5.Create());

            var match = plaintext.AsParallel().Where(p => ByteArrayCompare(targetBytes, md5.Value.ComputeHash(Encoding.Default.GetBytes(p)))).FirstOrDefault();
            return match;
        }
		public List<int> ForAll ()
		{
			var myData = new List<int> () { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

			var ints = new List<int> ();

			myData.AsParallel ().ForAll (x => ints.Add (x));

			return ints;
		}
		public List<int>  WithNatural ()
		{
			var myData = new List<int> () { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

			var data =
				(from value in myData.AsParallel ()
				 select value);

			return data.ToList ();
		}
        public IHttpActionResult Periodical(List<Periodical> periodicals, int weeks)
        {
            periodicals.AsParallel()
                .ForAll(periodical => SmallQuoteEstimator.Australia.PriceEstimator.PeriodicalPrice(periodical, weeks));

            return Ok(new
            {
                data = periodicals
            });
        }
Example #17
0
 private static List <NumberCount> MaxOccurrences2(List <int> integers)
 {
     return(integers?.AsParallel()
            .GroupBy(number => number)
            .Select(k => new NumberCount(k.Key, k.Count()))
            .GroupBy(x => x.Occurrences)
            .OrderByDescending(x => x.Key)
            .FirstOrDefault()?
            .ToList());
 }
 public List<IisApplication> ScanDirectory(string directory)
 {
     #if ASYNC_TEST
     Thread.Sleep(TimeSpan.FromSeconds(2));
     #endif
     var apps = new List<DirectoryInfo>();
     AddWebAppDirs(directory, apps);
     var result = apps.AsParallel().Select(GetApplicationInfo).ToList();
     return result;
 }
        public IHttpActionResult Equipment(List<EquipmentSupply> equipments)
        {
            equipments.AsParallel()
                .ForAll(equipment => SmallQuoteEstimator.Australia.PriceEstimator.EquipmentPrice(equipment));

            return Ok(new
            {
                data = equipments
            });
        }
Example #20
0
 private static void MaxOccurrences2(List <int> integers)
 {
     integers?.AsParallel()
     .GroupBy(x => x)               //group numbers, key is number, count is count
     .Select(k => new NumberCount(k.Key, k.Count()))
     .GroupBy(x => x.Occurrences)   //group by Occurrences, key is Occurrences, value is result
     .OrderByDescending(x => x.Key) //sort
     .FirstOrDefault()?             //the first one is result
     .ToList();
 }
Example #21
0
 private static List <NumberCount> MaxOccurrences1(List <int> integers)
 {
     return(integers?.AsParallel()
            .GroupBy(number => number)                      //group numbers, key is number, count is Occurrences
            .GroupBy(x => x.Count())                        //group by count, key is Occurrences, value is result
            .OrderByDescending(x => x.Key)
            .FirstOrDefault()?                              //the first the your result
            .ToList()                                       //get MaxOccurrences list
            .Select(k => new NumberCount(k.Key, k.Count())) //Grap result, before sort
            .ToList());
 }
 private static void MaxOccurrences1(List <int> integers)
 {
     integers?.AsParallel()
     .GroupBy(number => number)       //group numbers, key is number, count is count
     .GroupBy(x => x.Count())         //group by count, k
     .OrderByDescending(x => x.Key)
     .FirstOrDefault()?
     .ToList()                                       //get MaxOccurrences
     .Select(k => new NumberCount(k.Key, k.Count())) //Grap result, before sort
     .ToList();
 }
        public static void ShowParallelLinq(List<string> data)
        {
            OrderedParallelQuery<string> parallelGroups = data.AsParallel().OrderBy(item => item);

            // Show the total count of items still
            // matches the original count
            //System.Diagnostics.Trace.Assert(
            //  data.Count == parallelGroups.Sum(
            //      item => item.Count()));

        }
Example #24
0
 /// <summary>
 /// ドメインサービスの起動。
 /// </summary>
 private async void StartupDomainService()
 {
     try {
         Hosts = new List<ServiceHost>();
         Disposable.Create(() =>
         {
             Hosts
                 .AsParallel()
                 .Where(a => a.State == CommunicationState.Opened)
                 .ForAll(a => a.Close());
         }).Add(Tasks);
         {
             Debug.WriteLine("## host setting start ##");
             var host = new ServiceHost(typeof(SignpostService), Const.ServiceUrl);
             host
                 .AddServiceEndpoint(typeof(ISignpostContext), new BasicHttpBinding(), "");
             Hosts.Add(host);
             Debug.WriteLine("## host setting end ##");
         }
         {
             Debug.WriteLine("## host setting start ##");
             var host = new ServiceHost(typeof(StaticContentsService), Const.BaseUri);
             host
                 .AddServiceEndpoint(typeof(IStaticContents), new WebHttpBinding(), "")
                 .Behaviors.Add(new WebHttpBehavior());
             Hosts.Add(host);
             Debug.WriteLine("## host setting end ##");
         }
         foreach (var h in Hosts) {
             Debug.WriteLine("## host[{0}] Open...");
             await h.OpenAsync();
             Debug.WriteLine("## host[{0}] Opened. Status={1}, SingletonInstance={2}",
                 h.BaseAddresses.FirstOrDefault(),
                 h.State, h.SingletonInstance);
         }
         FinLoad();
     }
     catch (AddressAccessDeniedException aadEx) {
         var buf = new StringBuilder();
         buf.AppendLine(aadEx.ToString());
         buf.AppendLine();
         buf.AppendLine("----* 以下のコマンドを管理権限で実行してください *----");
         buf.AppendLine(string.Format(
             "netsh http add urlacl url={0}://+:{1}/ user={2}",
             Const.BaseUri.Scheme, Const.BaseUri.Port, Environment.UserName));
         Debug.WriteLine(buf.ToString());
     }
     catch (Exception ex) {
         var buf = new StringBuilder();
         buf.AppendLine(ex.ToString());
         Debug.WriteLine(buf.ToString());
     }
 }
Example #25
0
 private static long FindCommonAncestor(List<Person> population)
 {
     int populationCount = population.Count;
     var ancestors = ProduceAncestorIds(Ancestor.AncestorCount);
     return ancestors.AsParallel()
                     .Where(ancestor => population
                         .AsParallel()
                         .Where(person => person.Ancestors.Contains(ancestor))
                         .Count() == populationCount)
                     .DefaultIfEmpty(-1)
                     .First();
 }
Example #26
0
 private static void AsParallel2(List <int> integers)
 {
     integers?.AsParallel()
     .GroupBy(number => number)
     .Select(k => new
     {
         Key         = k.Key,
         Occurrences = k.Count()
     })                                     //Grap result, before sort
     .OrderByDescending(x => x.Occurrences) //sort after result
     .ToList();
 }
		public List<int>  CancellatioNatural ()
		{
			var cs = new CancellationTokenSource ();
			var myData = new List<int> () { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
			var data =
				(from Value in myData.AsParallel ().WithCancellation (cs.Token)
				 select PauseSelect (Value));

			Thread.Sleep (1);
			cs.Cancel ();

			return data.ToList ();
		}
Example #28
0
 private void RunTest(string queueName, int messageCount, int queueCount, ILogProvider logProvider)
 {
     var tasks = new List<Task>(queueCount);
     for (var i = 0; i < queueCount; i++)
     {
         var producer = new ProducerShared();
         var task = new Task(() => producer.RunTest<PostgreSqlMessageQueueInit, FakeMessage>(queueName, ConnectionInfo.ConnectionString, false, messageCount,
             logProvider, Helpers.GenerateData, Helpers.NoVerification, true, false));
         tasks.Add(task); 
     }
     tasks.AsParallel().ForAll(x => x.Start());
     Task.WaitAll(tasks.ToArray());
 }
		public List<int> CancellationExtension ()
		{
			var cs = new CancellationTokenSource ();
			var myData = new List<int> () { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
			var data = myData.AsParallel ().
			           Select (x => PauseSelect (x)).
				WithCancellation (cs.Token);

			Thread.Sleep (1);
			cs.Cancel ();

			return data.ToList ();
		}
Example #30
0
 public void UnloadFiles(List<IFileInfo> fiels, string folder)
 {
     foreach (var fileInfo in fiels.AsParallel())
     {
         try
         {
             _filesRepository.GetFileToFolder(fileInfo, folder);
         }
         catch (Exception)
         {
         }
     }
 }
        public static IEnumerable<Tuple<string, UniversalValue>> Distribution(this IList<UniversalValue> input, List<Tuple<UniversalValue, UniversalValue>> groups)
        {
            if (input == null)
                throw new ArgumentNullException("input");
            UniversalValue first = input.FirstOrDefault();
            double totalCount = input.Count();
            if (first == null)
                throw new Exception("Sequence contains no elements");
            if (first.Type != groups.First().Item1.Type)
                throw new Exception("Input type not equals to group type");

            switch (first.Type)
            {
                case UniversalValue.UniversalClassType.Numeric:
                    return groups.AsParallel().AsOrdered().Select(
                        g =>
                        new Tuple<string, UniversalValue>(("From " + g.Item1 + " to " + g.Item2).Replace(".",","),
                                                          new UniversalValue(
                                                              (input.Count(i => i.DoubleValue >= g.Item1.DoubleValue && i.DoubleValue < g.Item2.DoubleValue)
                                                              / totalCount)*100)));
                case UniversalValue.UniversalClassType.String:
                    return groups.AsParallel().AsOrdered().Select(
                        g =>
                        new Tuple<string, UniversalValue>(g.Item1.StringValue,
                                                          new UniversalValue(
                                                              (input.Count(i => i.StringValue == g.Item1.StringValue)
                                                              / totalCount) * 100)));
                case UniversalValue.UniversalClassType.TimeSpan:
                    return groups.AsParallel().AsOrdered().Select(
                        g =>
                            new Tuple<string, UniversalValue>(("From " + g.Item1 + " to " + g.Item2).Replace(".",","),
                                                          new UniversalValue(
                                                              (input.Count(i => i.TimespanValue >= g.Item1.TimespanValue && i.TimespanValue < g.Item2.TimespanValue)
                                                              / totalCount) * 100)));
                default:
                    throw new Exception("Percentile operarion not supported for " + first.Type);

            }
        }
        public void ShowParallelLinq(List<string> data)
        {
            ParallelQuery<IGrouping<char, string>> parallelGroups;
            parallelGroups =
                from text in data.AsParallel()
                orderby text
                group text by text[0];

            // Show the total count of items still
            // matches the original count
            //System.Diagnostics.Trace.Assert(
            //    data.Count == parallelGroups.Sum(
            //    item => item.Count()));
        }
Example #33
0
        /// <summary>
        /// Stops the specified workers.
        /// </summary>
        /// <param name="workers">The workers.</param>
        public void Stop(List<IWorker> workers)
        {
            Guard.NotNull(() => workers, workers);

            _waitForEventOrCancel.Set();
            _waitForEventOrCancel.Cancel();

            if (workers.Count == 0) return;

            //wait for workers to stop
            WaitForDelegate.Wait(() => workers.Any(r => r.Running), _configuration.TimeToWaitForWorkersToStop);
            if (workers.Any(r => r.Running))
            {
                workers.AsParallel().ForAll(w => w.AttemptToTerminate());
            }

            var alCancel = workers.Where(worker => worker.Running).ToList();

            //attempt to cancel workers if any are still running
            if (alCancel.Count > 0)
            {
                _cancelWorkSource.CancellationTokenSource.Cancel();
                WaitForDelegate.Wait(() => alCancel.Any(r => r.Running), _configuration.TimeToWaitForWorkersToCancel);
                if (alCancel.Any(r => r.Running))
                {
                    alCancel.AsParallel().ForAll(w => w.AttemptToTerminate());
                }
            }

            //force kill workers that are still running by aborting the thread, or waiting until work has completed
            var alForceTerminate = workers.Where(worker => worker.Running).ToList();
            if (alForceTerminate.Count > 0)
            {
                alForceTerminate.AsParallel().ForAll(w => w.TryForceTerminate());
            }

            //dispose workers - they are created by a factory, and must be disposed of
            foreach (var worker in workers)
            {
                try
                {
                    worker.Dispose();
                }
                catch (Exception e)
                {
                    _log.ErrorException("An error has occurred while disposing of a worker", e, null);
                }
            }
        }
Example #34
0
 public static void Pa()
 {
     Thread.Sleep(3000);
     List<string> names = new List<string>();
     names.Add("naynish"); names.Add("jay");
     names.Add("tripti"); names.Add("purab");
     Parallel.ForEach(names, Display);
     var result = from item in names.AsParallel()
                  select item;
     foreach (var item in result)
     {
         Console.WriteLine(item);
     }
     eve.Set();
 }
Example #35
0
        static void Main(string[] args)
        {
            WorkItems = new BlockingCollection<WorkItem>();

            var consumers = new List<Consumer>();
            consumers.Add(new Consumer());
            consumers.Add(new Consumer());
            consumers.Add(new Consumer());

            Task.Factory.StartNew(() =>
            {
                consumers.AsParallel().ForAll(x => x.Consume());
            });

            new Producer().Produce();
        }
Example #36
0
        public static void SaveSiteMap(List<SiteMapItemDb> siteMapsInfo, string fileName)
        {
            var sitemapGenerator = new SitemapGenerator();
            var xmlDoc =
                sitemapGenerator.GenerateSiteMap(
                    siteMapsInfo.AsParallel().Select(s => new SitemapItem(s.Url, s.LastModified, s.ChangeFrequency)));

            using (var compressedFileStream = new FileStream(fileName, FileMode.Create))
            {
                using (GZipStream compressionStream = new GZipStream(compressedFileStream,
                    CompressionMode.Compress))
                {
                    xmlDoc.Save(compressionStream);
                }
            }
        }
Example #37
0
        public static ParallelQuery<SteamApp> GetSteamApps()
        {
            var appIdsToRun = new List<int>();

            for (var i = MininumAppId; i < MaximumAppId; i++)
            {
                appIdsToRun.Add(i);
            }

            return appIdsToRun
                .AsParallel()
                .WithDegreeOfParallelism(MaximumCoresToOccupy)
                .Select(async appId => await GetSteamApp(appId))
                .Select(sa => sa.Result)
                .Where(sa => sa != null);
        }
Example #38
0
        /// <summary>
        /// 常用的线程及并行操作,Task,Parallel,这些默认都是通过ThreadPool开线程的
        /// </summary>
        private static void RunTypical_Thread_Task_Parallel_Demo()
        {
            //启动个线程
            Task.Factory.StartNew(Run);
            Task.Factory.StartNew(() =>
            {
                Run();
            });


            //启动个线程
            Task.Run(() => Run());


            //并行for
            Parallel.For(0, 10, item => Console.WriteLine(item));
            Parallel.For(0, 10, item =>
            {
                Console.WriteLine(item);
            });


            //设置最大并行度
            Parallel.For(0, 10, new ParallelOptions {
                MaxDegreeOfParallelism = 2
            }, item => Console.WriteLine(item));


            //并行foreach
            var lst = new List <int> {
                1, 2, 3, 4, 5
            };

            Parallel.ForEach(lst, item => Console.WriteLine(item));
            Parallel.ForEach(lst, Console.WriteLine);
            lst.AsParallel().ForAll(Console.WriteLine); //数组、集合都可以.AsParallel()

            //并行调用多个方法
            Parallel.Invoke(Run, Run, () => { Console.WriteLine(""); }, () => { });
        }
Example #39
0
        /// <summary>
        /// Stops and removes all registered services for the given application identifier and notification type
        /// </summary>
        /// <param name="applicationId">Application identifier.</param>
        /// <param name="waitForQueuesToFinish">If set to <c>true</c> waits for queues to be drained before returning.</param>
        /// <typeparam name="TNotification">The 1st type parameter.</typeparam>
        public void StopAllServices <TNotification>(string applicationId, bool waitForQueuesToFinish = true)
        {
            var type = typeof(TNotification);

            var stopping = new List <ServiceRegistration>();

            lock (serviceRegistrationsLock)
            {
                if (string.IsNullOrEmpty(applicationId))
                {
                    var services = from s in serviceRegistrations where s.NotificationType == type select s;

                    if (services != null && services.Any())
                    {
                        stopping.AddRange(services);
                    }

                    serviceRegistrations.RemoveAll(s => s.NotificationType == type);
                }
                else
                {
                    var services = from s in serviceRegistrations
                                   where s.NotificationType == type &&
                                   s.ApplicationId.Equals(applicationId)
                                   select s;

                    if (services != null && services.Any())
                    {
                        stopping.AddRange(services);
                    }

                    serviceRegistrations.RemoveAll(s => s.NotificationType == type && s.ApplicationId.Equals(applicationId));
                }
            }

            if (stopping != null && stopping.Any())
            {
                stopping.AsParallel().ForAll(sr => StopService(sr, waitForQueuesToFinish));
            }
        }
        static void Main(string[] args)
        {
            List <int> dates = new List <int>();

            Random rd = new Random();

            for (int i = 0; i < 1000000; i++)
            {
                dates.Add(rd.Next());
            }

            //计时器
            Stopwatch watch  = new Stopwatch();
            Stopwatch watch2 = new Stopwatch();

            watch.Start();

            var c = dates.Where(d => d % 3 == 0);

            Console.WriteLine(c.Count());


            watch.Stop();

            //PLinq
            watch2.Start();
            var c2 = dates.AsParallel().Where(d => d % 3 == 0);

            Console.WriteLine(c2.Count());


            watch2.Stop();


            //打印出运行花了多长时间(ms)
            Console.WriteLine("Linq" + watch.ElapsedMilliseconds + "ms");
            Console.WriteLine("PLinq" + watch2.ElapsedMilliseconds + "ms");

            Console.ReadKey();
        }
        private static IEnumerable <FileEntry> ExtractZipFile(FileEntry fileEntry)
        {
            List <FileEntry> files = new List <FileEntry>();

            ZipFile zipFile = null;

            try
            {
                zipFile = new ZipFile(fileEntry.Content);
            }
            catch (Exception e)
            {
                Console.WriteLine($"Failed to read from {fileEntry.FullPath} {e.Message} {e.StackTrace}");
            }
            if (zipFile != null)
            {
                List <ZipEntry> entries = new List <ZipEntry>();
                foreach (ZipEntry zipEntry in zipFile)
                {
                    entries.Add(zipEntry);
                }
                entries.AsParallel().ForAll(zipEntry =>
                {
                    if (!zipEntry.IsDirectory &&
                        !zipEntry.IsCrypted &&
                        zipEntry.CanDecompress)
                    {
                        using var memoryStream = new MemoryStream();
                        byte[] buffer          = new byte[BUFFER_SIZE];
                        var zipStream          = zipFile.GetInputStream(zipEntry);
                        StreamUtils.Copy(zipStream, memoryStream, buffer);

                        var newFileEntry = new FileEntry(zipEntry.Name, fileEntry.FullPath, memoryStream);
                        files.AddRange(ExtractFile(newFileEntry));
                    }
                });
            }

            return(files);
        }
        private static void MapReduceTest()
        {
            //Maps only positive number from list
            Func <int, IEnumerable <int> > mapPositiveNumbers = number =>
            {
                IList <int> positiveNumbers = new List <int>();
                if (number > 0)
                {
                    positiveNumbers.Add(number);
                }
                return(positiveNumbers);
            };

            // Group results together
            Func <int, int> groupNumbers = value => value;
            //Reduce function that counts the occurence of each number
            Func <IGrouping <int, int>, IEnumerable <KeyValuePair <int, int> > > reduceNumbers =
                grouping => new[] { new KeyValuePair <int, int>(grouping.Key, grouping.Count()) };

            // Generate a list of random numbers between -10 and 10
            IList <int> sourceData = new List <int>();
            var         rand       = new Random();

            for (int i = 0; i < 1000; i++)
            {
                sourceData.Add(rand.Next(-10, 10));
            }

            // Use MapReduce function
            var result = sourceData.AsParallel().MapReduce(
                mapPositiveNumbers,
                groupNumbers,
                reduceNumbers);

            // process the results
            foreach (var item in result)
            {
                Console.WriteLine($"{item.Key} found {item.Value} times");
            }
        }
Example #43
0
        public void Start(string taskName)
        {
            if (this.LoadedTasks.FirstOrDefault(x => x.taskName == taskName) == null)
            {
                throw new PvcException("Task {0} not defined.", taskName);
            }

            var dependencyGraph = new PvcDependencyGraph();

            foreach (var task in this.LoadedTasks)
            {
                dependencyGraph.AddDependencies(task.taskName, task.dependentTaskNames);
            }

            var executionPaths = new List <IEnumerable <PvcTask> >();
            var runPaths       = dependencyGraph.GetPaths(taskName);

            foreach (var runPath in runPaths)
            {
                var runTasks = runPath.Select(x => this.LoadedTasks.First(y => y.taskName == x));
                executionPaths.Add(runTasks);
                foreach (var runTask in runTasks)
                {
                    // create locks
                    locks.AddOrUpdate(runTask.taskName, new { }, (s, o) => o);
                }
            }

            try
            {
                foreach (var executionPath in executionPaths.AsParallel())
                {
                    this.RunTasks(executionPath.ToArray());
                }
            }
            catch (AggregateException ex)
            {
                throw new PvcException(ex.InnerException);
            }
        }
Example #44
0
        public void ProcessTweet(List <Tweet> tweets)
        {
            string str = string.Empty;


            Parallel.Invoke
            (
                () => { StaticticalAverages.Process(tweets.Count); },
                () =>
            {
                var tweetsWithHashtag = tweets.AsParallel()
                                        .Where(t => t.Data?.HasHashTag == true)
                                        .SelectMany(t => t.Data.Collection.HashTags.Select(ht => ht.Tag.ToLower()));
                HashtagStatictics.AddToAttributeTracker(tweetsWithHashtag);
            },
                () =>
            {
                foreach (Tweet tweet in tweets)
                {
                    if (string.IsNullOrEmpty(tweet.Data?.Text))
                    {
                        continue;
                    }
                    var emojisInTweets = _emojiService.GetEmojisFromTweet(tweet.Data.Text);
                    EmojiStatictics.AddToAttributeTracker(emojisInTweets);
                    List <string> domains = new List <string>();
                    if (tweet.Data.HasUrl)
                    {
                        domains.AddRange(tweet.Data.Collection.Urls.Select(x =>
                        {
                            var result = x.Display_Url.Split('/');
                            return(result[0]);
                        }).ToList());

                        UrlStatictics.AddUrlProperties(domains);
                    }
                }
            }
            );
        }
    public Tuple <bool, bool> ExistConfig(List <string> files)
    {
        bool webApiFiltersConfig = false;
        bool log4NetConfig       = false;

        files.AsParallel()
        .ForAll(file =>
        {
            string contentFile = File.ReadAllText(file);

            if (!webApiFiltersConfig)
            {
                webApiFiltersConfig = contentFile.Contains("Action4LogFilter");
            }
            if (!log4NetConfig)
            {
                log4NetConfig = contentFile.Contains("log4net") && contentFile.Contains("XmlConfigurator.Configure");
            }
        });

        return(new Tuple <bool, bool>(webApiFiltersConfig, log4NetConfig));
    }
Example #46
0
        public void getBitArrayFromByteArr(List <byte> inputArray)
        {
            intraBitArray = new List <bool>();
            Func <byte, List <bool> > transformByteToListBools = new Func <byte, List <bool> >((element) =>
            {
                var tempList = new List <bool>()
                {
                    false, false, false, false, false, false, false, false
                };
                for (int i = 0; i < 8; i++)
                {
                    tempList[i] = FirstSectionOfOperations.getIbit(element, i) == 1 ? true : false;
                }
                tempList.Reverse();
                return(tempList);
            });

            inputArray.Reverse();
            intraBitArray = (from element in inputArray.AsParallel() select transformByteToListBools(element)).SelectMany(x => x).ToList();

            IsReadOnly = false;
        }
Example #47
0
        public static IEnumerable <Mobile> GetEnumeratedMobiles(this Region region)
        {
            if (region == null)
            {
                yield break;
            }

            List <Mobile>        list = region.GetMobiles();
            IEnumerable <Mobile> e;

            lock (_MobileLock)
            {
                e = list.AsParallel().Where(m => m != null && m.Region.IsPartOf(region));
            }

            foreach (Mobile m in e)
            {
                yield return(m);
            }

            ColUtility.Free(list);
        }
Example #48
0
        protected override void Solve(out string answer)
        {
            //Produce all combinations
            List <Pair> list = new List <Pair>();

            foreach (long a in Enumerable.Range(-999, 2 * 999 + 1))
            {
                foreach (long b in Enumerable.Range(-1000, 2 * 1000 + 1))
                {
                    list.Add(new Pair(a: a, b: b));
                }
            }

            //Find primes
            list.AsParallel().ForAll(pair => pair.FindPrimes());

            //Get the pair with longest count of primes produced
            var ordered = list.OrderByDescending(pair => pair.NumPrimes);
            var output  = ordered.First();

            answer = $"List size {list.Count}. The product of coefficients a ({output.A}) and b ({output.B}) is {output.CoefProduct}. Number of primes this pair generated is: {output.NumPrimes}.";
        }
Example #49
0
        /// <summary>
        /// Apparently, there are many files that are not listed in the patchlist.
        /// I assume these are legacy files that are not needed anymore and can be safely deleted!
        /// My game client was updated ever since Episode 2: using this method weeds out 1300+ files (1GB).
        /// </summary>
        /// <returns></returns>
        private async Task CleanLegacyFiles(List <PatchInfo> patchlist, List <string> gameFiles)
        {
            Output.OnLegacyFilesScanning();

            var t = Stopwatch.StartNew();

            var requiredFiles = patchlist.AsParallel().Select(Q =>
            {
                var shortpath = Q.File.Replace('/', '\\');
                return(Path.Combine(Settings.GameDirectory, shortpath));
            });

            // Only delete files in /data/win32 for safety. Prevents deleting weird stuffs like GameGuard or Tweaker stuffs.
            // EnumerateGameFiles is only sending out files in /data/win32 and several whitelisted files in pso2_bin.

            var legacyFiles = gameFiles
                              .AsParallel()
                              .Except(requiredFiles)
                              .ToList();

            Output.Benchmark(t, "Scan legacy files");

            if (legacyFiles.Any() == false)
            {
                Output.OnLegacyFilesNotFound();
                return;
            }

            Output.OnLegacyFilesFound(legacyFiles);
            await Task.Run(() =>
            {
                foreach (var file in legacyFiles)
                {
                    File.Delete(file);
                }
            });

            Output.Benchmark(t, "Destroy legacy files");
        }
Example #50
0
        private void LoadNoteList(Guid notebookGategory)
        {
            List <Core.Models.Note> notes = new List <Core.Models.Note>(_noteService.GetNotes(notebookGategory).OrderByDescending(p => p.UpdatedDateTime));

            // apply search filter
            if (_searchService.IsSearchInProgress() == true)
            {
                notes = notes.AsParallel().Where(p => p.Title.IndexOf(_searchService.SearchTerm, StringComparison.InvariantCultureIgnoreCase) != -1 ||
                                                 p.Content.IndexOf(_searchService.SearchTerm, StringComparison.InvariantCultureIgnoreCase) != -1).ToList();
            }

            if (notes.Count > 0)
            {
                if (_selectedNoteFromSearch != Guid.Empty)
                {
                    RestoreSelectedNoteOnSearch(notes);
                }
                else
                {
                    SelectedNote = notes[0]; // select the first available note
                }
                StoreSelectedNoteOnSearch(SelectedNote.Id);
            }
            else
            {
                SelectedNote = null;                                       // no notes found in the selected category
            }
            NoteList = new ObservableCollection <Core.Models.Note>(notes); // bind it to the live NoteList
            ShowNotesNotFoundPanel = (NoteList.Count == 0);

            if (String.IsNullOrEmpty(_searchService.SearchTerm) == true)
            {
                ShowAddNoteButton = (SelectedNotebookCategoryId == NotebookCategories.Default);
            }
            else
            {
                ShowAddNoteButton = false;
            }
        }
Example #51
0
        private static int parallelDequeue(BlockingCollection <IVertex> queue, int[] distances, object lockObject)
        {
            int visited = 0;

            IVertex currentNode = null;

            while (currentNode == null)
            {
                queue.TryTake(out currentNode);
            }


            if (currentNode != null)
            {
                int[]        sums  = new int[currentNode.OutgoingEdges.Count * 2];
                List <IEdge> edges = currentNode.OutgoingEdges.ToList();


                Interlocked.Add(ref visited, edges.AsParallel().Sum(edge => processEdges(edge, currentNode, queue, distances)));
            }
            return(visited);
        }
Example #52
0
        /// <summary>
        /// Flickers the lights to the alarm state, and then back to where there were before
        /// </summary>
        public void Alert()
        {
            // Get all the current lights that are on
            onLights = new List <HueLight>();
            IEnumerable <Light> allLights = client.GetLightsAsync().GetAwaiter().GetResult();

            foreach (Light light in allLights)
            {
                if (light.State.On)
                {
                    onLights.Add(new HueLight
                    {
                        Light         = light,
                        PreviousState = new LightCommand()
                        {
                            Hue = light.State.Hue, Saturation = light.State.Saturation, Brightness = light.State.Brightness
                        }
                    });
                }
            }

            // Flicker 'em
            if (onLights.Count > 0)
            {
                onLights
                .AsParallel()
                .WithDegreeOfParallelism(onLights.Count)
                .ForAll(hueLight =>
                {
                    client.SendCommandAsync(alarmState, new List <string> {
                        hueLight.Light.Id
                    }).GetAwaiter().GetResult();
                    Thread.Sleep(500);
                    client.SendCommandAsync(hueLight.PreviousState, new List <string> {
                        hueLight.Light.Id
                    }).GetAwaiter().GetResult();
                });
            }
        }
Example #53
0
        public override async Task <IEnumerable <SoundElement> > GetSoundElements()
        {
            List <RawHitObject> hitObjects = _osuFile.HitObjects.HitObjectList;
            var elements = new ConcurrentBag <SoundElement>();

            var dirInfo = new DirectoryInfo(_sourceFolder);
            var waves   = new HashSet <string>(dirInfo.EnumerateFiles()
                                               .Where(k => Information.SupportExtensions.Contains(
                                                          k.Extension, StringComparer.OrdinalIgnoreCase)
                                                      )
                                               .Select(p => Path.GetFileNameWithoutExtension(p.Name))
                                               );

            await Task.Run(() =>
            {
                hitObjects.AsParallel()
                .WithDegreeOfParallelism(/*Environment.ProcessorCount > 1 ? Environment.ProcessorCount - 1 :*/ 1)
                .ForAll(obj => { AddSingleHitObject(obj, waves, elements).Wait(); });
            }).ConfigureAwait(false);

            return(new List <SoundElement>(elements));
        }
Example #54
0
        /// <summary>
        /// Filters tags based on settings specified in flags
        ///        0b00000001 : Hide AniDB Internal Tags
        ///        0b00000010 : Hide Art Style Tags
        ///        0b00000100 : Hide Source TransactionHelper.Work Tags
        ///        0b00001000 : Hide Useful Miscellaneous Tags
        ///        0b00010000 : Hide Plot Spoiler Tags
        ///        0b00100000 : Hide Settings Tags
        /// </summary>
        /// <param name="strings">A list of strings [ "meta tags", "elements", "comedy" ]</param>
        /// <param name="flags">the <see cref="TagFilter.Filter"/> flags</param>
        /// <param name="addTags">is it okay to add tags to the list</param>
        /// <returns>the original list with items removed based on rules provided</returns>
        public static List <string> ProcessTags(Filter flags, List <string> strings, bool addTags = true)
        {
            if (strings.Count == 0)
            {
                return(strings);
            }

            List <string> toAdd = new List <string>();

            if (strings.Count == 1)
            {
                if (IsTagBlackListed(strings[0], flags, ref toAdd))
                {
                    strings.Clear();
                }
                return(strings);
            }

            List <string> toRemove = new List <string>((int)Math.Ceiling(strings.Count / 2D));

            var stringsSet = new HashSet <string>(strings);

            strings.AsParallel().ForAll(a => MarkTagsForRemoval(a, flags, ref toRemove, ref toAdd));

            foreach (var a in toRemove)
            {
                if (stringsSet.Contains(a))
                {
                    strings.Remove(a);
                }
            }

            if (addTags)
            {
                toAdd.ForEach(strings.Add);
            }

            return(strings);
        }
Example #55
0
        static void Main(string[] args)
        {
            List <string> listOfStrings = new List <string>
            {
                "Anna", "Bertil", "Cecilia", "David", "Elin", "Fredrik", "Greta", "Håkan", "Ines", "Johan"
            };

            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();
            var resultSet = listOfStrings
                            .AsParallel() // för att få asynkront i en foreach (allt exekveras samtidigt)
                            .Select(ReverseString);

            foreach (string name in resultSet)
            {
                Console.WriteLine(name);
            }
            stopWatch.Stop();

            Console.WriteLine($"{Environment.NewLine}Det tog {stopWatch.ElapsedMilliseconds / 1000.0} sekunder.");
        }
        private void RunTest(string queueName, int messageCount, int queueCount, ILogProvider logProvider, string connectionString, LinqMethodTypes linqMethodTypes, ICreationScope scope)
        {
            var tasks = new List <Task>(queueCount);

            for (var i = 0; i < queueCount; i++)
            {
                var id       = Guid.NewGuid();
                var producer = new ProducerMethodShared();
                if (linqMethodTypes == LinqMethodTypes.Compiled)
                {
                    tasks.Add(new Task(() => producer.RunTestCompiled <SqLiteMessageQueueInit>(queueName, connectionString, false, messageCount,
                                                                                               logProvider, Helpers.GenerateData, Helpers.NoVerification, true, false, id, GenerateMethod.CreateCompiled, 0, scope)));
                }
                else
                {
                    tasks.Add(new Task(() => producer.RunTestDynamic <SqLiteMessageQueueInit>(queueName, connectionString, false, messageCount,
                                                                                              logProvider, Helpers.GenerateData, Helpers.NoVerification, true, false, id, GenerateMethod.CreateDynamic, 0, scope)));
                }
            }
            tasks.AsParallel().ForAll(x => x.Start());
            Task.WaitAll(tasks.ToArray());
        }
Example #57
0
        static void Main(string[] args)
        {
            List <Student> list = new List <Student>()
            {
                new Student()
                {
                    ID = 1, Name = "jack", Age = 20
                },
                new Student()
                {
                    ID = 1, Name = "mary", Age = 25
                },
                new Student()
                {
                    ID = 1, Name = "joe", Age = 29
                },
                new Student()
                {
                    ID = 1, Name = "Aaron", Age = 25
                },
            };

            var map = list.AsParallel().ToLookup(i => i.Age, count => 1);

            var reduce = from IGrouping <int, int> singleMap
                         in map.AsParallel()
                         select new
            {
                Age   = singleMap.Key,
                Count = singleMap.Count()
            };

            reduce.ForAll(i =>
            {
                Console.WriteLine("Age={0} Count:{1}", i.Age, i.Count);
            });

            Console.Read();
        }
Example #58
0
        private Task conventWordToPdf(List <string> list)
        {
            return(Task.Factory.StartNew(() =>
            {
                PDFConverter pdfConverter = new PDFConverter();
                list.AsParallel()
                .ForAll(item =>
                {
                    byte[] pdf = pdfConverter.GetPDF(item);
                    string ext = Path.GetExtension(item);
                    File.WriteAllBytes(item.Replace(ext, ".pdf"), pdf);

                    BeginInvoke(new Action(() =>
                    {
                        lstFile.Items.Remove(item);
                    }));
                });

                pdfConverter.Dispose();
                pdfConverter = null;
            }));
        }
Example #59
0
        public static void DoForeach()
        {
            List <int> list = new List <int>();

            for (int i = 0; i < 100; i++)
            {
                list.Add(i);
            }

            DateTime dt = DateTime.Now;

            //foreach (var x in list)
            //{
            //    System.Threading.Thread.Sleep(100);
            //    Console.WriteLine(x);
            //}

            var ls  = list.Select(x => x + 10);
            var ls2 = list.AsParallel().Select(x => x + 10);

            foreach (var item in ls2)
            {
                Console.WriteLine(item);
            }

            //Task.Factory.StartNew(() => Console.WriteLine("Hello."));

            //Parallel.ForEach(list, x =>
            //{
            //    System.Threading.Thread.Sleep(100);

            //    Console.WriteLine(x);
            //});

            //double time = (DateTime.Now - dt).TotalMilliseconds;

            //Console.WriteLine(time);
            //Console.Read();
        }
Example #60
0
        public List <uint256> FetchAllRawDataThenParallelDeserialize()
        {
            using (var engine = this.GetDBEngine())
            {
                using (Transaction transaction = engine.GetTransaction())
                {
                    transaction.ValuesLazyLoadingIsOn = false;

                    var result    = new uint256[this.data.Length];
                    var rawValues = new List <byte[]>(this.data.Length);

                    foreach (uint256 input in this.data)
                    {
                        Row <byte[], byte[]> row = transaction.Select <byte[], byte[]>("Coins", input.ToBytes(false));

                        rawValues.Add(row.Value);
                    }

                    return(rawValues.AsParallel().Select(rawValue => this.dBreezeSerializer.Deserialize <uint256>(rawValue)).ToList());
                }
            }
        }