コード例 #1
0
            static SampleBreathing UseMedianaFilter(ConcurrentQueue <SampleBreathing> q)
            {
                var listFlow = q.AsParallel().Select(x => x.Flow.ParameterValue.Value).ToList();
                var listO2   = q.AsParallel().Select(x => x.O2.ParameterValue.Value).ToList();
                var listCO2  = q.AsParallel().Select(x => x.CO2.ParameterValue.Value).ToList();

                listFlow.Sort();
                listO2.Sort();
                listCO2.Sort();
                return(new SampleBreathing(listFlow[listFlow.Count / 2], listO2[listO2.Count / 2], listCO2[listCO2.Count / 2]));
            }
コード例 #2
0
        public static void TestMethod1()
        {
            ConcurrentQueue <string> list = new ConcurrentQueue <string>();

            list.Enqueue("A");
            list.Enqueue("B");
            list.Enqueue("C");
            list.Enqueue("D");
            list.Enqueue("A");
            list.Enqueue("D");

            Console.WriteLine("Select.......");
            list.AsParallel().Select(p => new
            {
                Name  = p,
                Count = 1
            }).ForAll((p) =>
            {
                Console.WriteLine("{0}\t{1}", p.Name, p.Count);
            });

            Console.WriteLine("ILookup.......");
            /*map操作生成的键值对由一个单词和数量1组成,该代码意在将每个单词作为键并将1作为值加入*/
            ILookup <string, int> map = list.AsParallel().ToLookup(p => p, k => 1);

            foreach (var v in map)
            {
                Console.Write(v.Key);
                foreach (int val in v)
                {
                    Console.WriteLine("\t{0}", val);
                }
            }
            /*reduce操作单词出现的次数*/
            var reduce = from IGrouping <string, int> reduceM in map.AsQueryable()
                         select new
            {
                key   = reduceM.Key,
                count = reduceM.Count()
            };

            Console.WriteLine("IGrouping.......");
            foreach (var v in reduce)
            {
                Console.Write(v.key);
                Console.WriteLine("\t{0}", v.count);
            }

            Console.ReadLine();
        }
コード例 #3
0
        public void display()
        {
            ConcurrentQueue <Rectangle> testList = new ConcurrentQueue <Rectangle>();

            Parallel.For(20, 300000000, n =>
            {
                testList.Enqueue(new Rectangle
                {
                    Width  = n,
                    Height = n
                });
            });
            Stopwatch watch = new Stopwatch();

            watch.Restart();
            var q1 = from x in testList
                     select x.Width * x.Height;

            watch.Stop();
            System.Console.WriteLine("Normal mode, it takes:{0} ms", watch.ElapsedMilliseconds);
            watch.Restart();
            var q2 = from x in testList.AsParallel()
                     select x.Width *x.Height;

            watch.Stop();
            System.Console.WriteLine("Parallel mode, it takes:{0} ms", watch.ElapsedMilliseconds);
        }
コード例 #4
0
 /// <summary>
 /// Sorts pending tasks descending by priority <see cref="Priority"/>
 /// </summary>
 protected void SortPendingTasks()
 {
     pendingTasks = new ConcurrentQueue <PrioritizedLimitedTask>(
         pendingTasks.AsParallel()
         .WithDegreeOfParallelism(Environment.ProcessorCount)
         .OrderByDescending(x => x.Priority, new PriorityComparer()));
 }
コード例 #5
0
        private void ConcurrentQueue()
        {
            ConcurrentQueue <int> queue = new ConcurrentQueue <int>();

            INumQuery.AsParallel <int>().ForAll(val => queue.Enqueue(val));

            queue.AsParallel().ForAll(v => Console.WriteLine(v.ToString()));
        }
コード例 #6
0
        public static void TestMethod1()
        {
            ConcurrentQueue <Product> products = new ConcurrentQueue <Product>();

            /*向集合中添加多条数据*/
            Parallel.For(0, 600000, (num) =>
            {
                products.Enqueue(new Product()
                {
                    Category = "Category" + num, Name = "Name" + num, SellPrice = num
                });
            });

            CancellationTokenSource cts = new CancellationTokenSource();
            /*创建tk1 任务  查询 符合 条件的数据*/
            Task <ParallelQuery <Product> > tk1 = new Task <ParallelQuery <Product> >((ct) =>
            {
                Console.WriteLine("开始执行 tk1 任务", products.Count);
                Console.WriteLine("tk1 任务中 数据结果集数量为:{0}", products.Count);
                var result = products.AsParallel().Where(p => p.Name.Contains("1") && p.Name.Contains("2"));
                return(result);
            }, cts.Token);

            /*创建tk2 任务,在执行tk1任务完成  基于tk1的结果查询 符合 条件的数据*/
            Task <ParallelQuery <Product> > tk2 = tk1.ContinueWith <ParallelQuery <Product> >((tk) =>
            {
                Console.WriteLine("开始执行 tk2 任务", products.Count);
                Console.WriteLine("tk2 任务中 数据结果集数量为:{0}", tk.Result.Count());
                var result = tk.Result.Where(p => p.Category.Contains("1") && p.Category.Contains("2"));
                return(result);
            }, TaskContinuationOptions.OnlyOnRanToCompletion);

            /*创建tk3 任务,在执行tk1任务完成  基于tk1的结果查询 符合 条件的数据*/
            Task <ParallelQuery <Product> > tk3 = tk1.ContinueWith <ParallelQuery <Product> >((tk) =>
            {
                Console.WriteLine("开始执行 tk3 任务", products.Count);
                Console.WriteLine("tk3 任务中 数据结果集数量为:{0}", tk.Result.Count());
                var result = tk.Result.Where(p => p.SellPrice > 1111 && p.SellPrice < 222222);
                return(result);
            }, TaskContinuationOptions.OnlyOnRanToCompletion);

            tk1.Start();

            Task.WaitAll(tk1, tk2, tk3);
            Console.WriteLine("tk2任务结果输出,筛选后记录总数为:{0}", tk2.Result.Count());
            Console.WriteLine("tk3任务结果输出,筛选后记录总数为:{0}", tk3.Result.Count());

            tk1.Dispose();
            tk2.Dispose();
            tk3.Dispose();
            cts.Dispose();
            Console.ReadLine();
        }
コード例 #7
0
        public static void TestMethod1()
        {
            ConcurrentQueue <Product> products = new ConcurrentQueue <Product>();

            /*向集合中添加多条数据*/
            Parallel.For(0, 6000000, (num) =>
            {
                products.Enqueue(new Product()
                {
                    Category = "Category" + num, Name = "Name" + num, SellPrice = num
                });
            });


            /*采用并行化整个查询 查询符合条件的数据*/
            Stopwatch sw = new Stopwatch();

            sw.Restart();
            var productListLinq = from product in products.AsParallel().WithExecutionMode(ParallelExecutionMode.ForceParallelism)
                                  where (product.Name.Contains("1") && product.Name.Contains("2") && product.Category.Contains("1") && product.Category.Contains("2"))
                                  select product;

            Console.WriteLine($"采用并行化整个查询 查询得出数量为:{productListLinq.Count()}");
            sw.Stop();
            Console.WriteLine($"采用并行化整个查询 耗时:{sw.ElapsedMilliseconds}");


            /*采用默认设置 由.NET进行决策 查询符合条件的数据*/
            sw.Restart();
            var productListPLinq = from product in products.AsParallel().WithExecutionMode(ParallelExecutionMode.Default)
                                   where (product.Name.Contains("1") && product.Name.Contains("2") && product.Category.Contains("1") && product.Category.Contains("2"))
                                   select product;

            Console.WriteLine($"采用默认设置 由.NET进行决策 查询得出数量为:{productListPLinq.Count()}");
            sw.Stop();
            Console.WriteLine($"采用默认设置 由.NET进行决策 耗时:{sw.ElapsedMilliseconds}");
            Console.ReadLine();
        }
コード例 #8
0
        static void Main(string[] args)
        {
            Console.ReadLine();
            Console.WriteLine("Started");

            var cts = new System.Threading.CancellationTokenSource();
            var ct  = cts.Token;

            var sw = Stopwatch.StartNew();

            Keys = new ConcurrentQueue <String>();

            var tAsync = new Task(() => ParallelPartitionGenerateAESKeysWCP(ct, 'A'));

            tAsync.Start();

            // Do something else
            // Wait for tAsync to finish
            tAsync.Wait();

            // Define the query indicating that it should run in parallel
            var keysWith10Letters = from key in Keys.AsParallel()
                                    where (CountLetters(key) >= 10) &&
                                    (key.Contains('A')) &&
                                    (key.Contains('F')) &&
                                    (key.Contains('9')) &&
                                    (!key.Contains('B'))
                                    orderby key
                                    select key;

            // Write some of the results of executing the query
            // Remember that the PLINQ query is going to be executed at this point
            // when the code requires results
            var keysList = keysWith10Letters.ToList();

            Console.WriteLine("The code generated {0} keys with at least ten letters, A, F and 9 but no B in the hexadecimal code.",
                              keysList.Count());
            Console.WriteLine("First key: {0} ",
                              keysList.ElementAt(0));
            Console.WriteLine("Last key: {0} ",
                              keysList.ElementAt(keysWith10Letters.Count() - 1));

            Console.WriteLine("Finished in {0}", sw.Elapsed.ToString());

            Console.ReadLine();
        }
コード例 #9
0
 private void DBHandler_SaveResultCallback(IEnumerable <KeyType> success = null, IEnumerable <KeyType> fails = null)
 {
     if (success != null)
     {
         foreach (var key in success)
         {
             if (IndexData.ContainsKey(key))
             {
                 var cData = IndexData[key];
                 cData.IsNeedToWrite  = false;
                 cData.SaveRetryTimes = 0;
             }
             Remove(key);
         }
     }
     if (fails != null)
     {
         foreach (var key in fails)
         {
             CacheDataInfo <KeyType, ValueType> cData = null;
             if (IndexData.ContainsKey(key))
             {
                 cData = IndexData[key];
             }
             else
             {
                 cData = CacheData.AsParallel().Where(o => o.Key.Equals(key)).FirstOrDefault();
             }
             if (cData != null)
             {
                 TotalRetryTimes      += 1;
                 cData.SaveRetryTimes += 1;
                 if (cData.SaveRetryTimes < Cache <KeyType, ValueType> .MaxSaveRetryTimes)
                 {
                     Add(ref cData, true);
                 }
                 else
                 {
                     OnDiscardData?.Invoke(cData);
                 }
             }
         }
     }
 }
コード例 #10
0
        public override void ReturnWord(string word)
        {
            ConcurrentQueue <string> wordSplited = new ConcurrentQueue <string>();

            word.Select(c => c.ToString()).ToList().ForEach(letter => wordSplited.Enqueue(letter));
            if (word.Length <= int.MaxValue)
            {
                wordSplited.AsParallel().WithDegreeOfParallelism(20).ForAll((character) =>
                {
                    wordSplited.TryDequeue(out character);
                    _getValue.Get(character);
                }
                                                                            );
            }
            else
            {
                _messaging.InvalidWord();
            }
        }
コード例 #11
0
        public static void TestMethod1()
        {
            ConcurrentQueue <Product> products = new ConcurrentQueue <Product>();

            /*向集合中添加多条数据*/
            Parallel.For(0, 1000, (num) =>
            {
                products.Enqueue(new Product()
                {
                    Category = "Category" + num, Name = "Name" + num, SellPrice = num
                });
            });

            products.AsParallel().Where(P => P.Name.Contains("1") && P.Name.Contains("2") && P.Name.Contains("3")).ForAll(product =>
            {
                Console.WriteLine("Name:{0}", product.Name);
            });

            Console.ReadLine();
        }
コード例 #12
0
        static void Main(string[] args)
        {
            Console.ReadLine();
            Console.WriteLine("Started");

            var cts = new System.Threading.CancellationTokenSource();
            var ct  = cts.Token;

            var sw = Stopwatch.StartNew();

            Keys = new ConcurrentQueue <String>();

            var tAsync = new Task(() => ParallelPartitionGenerateAESKeysWCP(ct, 'A'));

            tAsync.Start();

            // Do something else
            // Wait for tAsync to finish
            tAsync.Wait();

            // Define the query indicating that it should run in parallel
            var keysWith10Letters = from key in Keys.AsParallel()
                                    .WithMergeOptions(ParallelMergeOptions.NotBuffered)
                                    where (CountLetters(key) >= 10) &&
                                    (key.Contains('A')) &&
                                    (key.Contains('F')) &&
                                    (key.Contains('9')) &&
                                    (!key.Contains('B'))
                                    select key;

            // Write some of the results of executing the query
            // Remember that the PLINQ query is going to be executed at this point
            // when the code requires results
            Console.WriteLine("First key: {0} ",
                              keysWith10Letters.ElementAt(0));
            Console.WriteLine("First result shown in {0}", sw.Elapsed.ToString());

            Console.ReadLine();
        }
コード例 #13
0
        public static void TestMethod1()
        {
            ConcurrentQueue <Product> products = new ConcurrentQueue <Product>();

            /*向集合中添加多条数据  可以修改数据量查看Linq和Plinq的性能*/
            Parallel.For(0, 600000, (num) =>
            {
                products.Enqueue(new Product()
                {
                    Category = "Category" + num, Name = "Name" + num, SellPrice = num
                });
            });


            /*采用LINQ查询符合条件的数据*/
            var sw = new Stopwatch();

            sw.Restart();
            var productListLinq = from product in products
                                  where (product.Name.Contains("1") && product.Name.Contains("2") && product.Category.Contains("1") && product.Category.Contains("2"))
                                  select product;

            Console.WriteLine($"采用Linq 查询得出数量为:{productListLinq.Count()}");
            sw.Stop();
            Console.WriteLine($"采用Linq 耗时:{sw.ElapsedMilliseconds}");


            /*采用PLINQ查询符合条件的数据*/
            sw.Restart();
            var productListPLinq = from product in products.AsParallel() /*AsParallel 试图利用运行时所有可用的逻辑内核,从而使运行的速度比串行的版本要快 但是需要注意开销所带来的性能损耗*/
                                   where (product.Name.Contains("1") && product.Name.Contains("2") && product.Category.Contains("1") && product.Category.Contains("2"))
                                   select product;

            Console.WriteLine($"采用PLinq 查询得出数量为:{productListPLinq.Count()}");
            sw.Stop();
            Console.WriteLine($"采用PLinq 耗时:{sw.ElapsedMilliseconds}");
            Console.ReadLine();
        }
コード例 #14
0
        public static void TestMethod1()
        {
            ConcurrentQueue <int> products = new ConcurrentQueue <int>();

            /*向集合中添加多条数据*/
            Parallel.For(0, 6000000, (num) =>
            {
                products.Enqueue(num);
            });

            /*采用LINQ 返回 IEumerable<int>*/
            var productListLinq = (from product in products
                                   select product).Average();

            Console.WriteLine("采用Average计算平均值:{0}", productListLinq);

            /*采用PLINQ 返回 ParallelQuery<int>*/
            var productListPLinq = (from product in products.AsParallel()
                                    select product).Average();

            Console.WriteLine("采用Average计算平均值:{0}", productListPLinq);
            Console.ReadLine();
        }
コード例 #15
0
        public static void TestMethod1()
        {
            ConcurrentQueue <Product> products = new ConcurrentQueue <Product>();

            /*向集合中添加多条数据*/
            Parallel.For(0, 600000, (num) =>
            {
                products.Enqueue(new Product()
                {
                    Category = "Category" + num, Name = "Name" + num, SellPrice = num
                });
            });

            CancellationTokenSource cts   = new CancellationTokenSource();
            CancellationToken       token = cts.Token;
            /*创建tk1 任务  查询 符合 条件的数据*/
            Task <ParallelQuery <Product> > tk1 = new Task <ParallelQuery <Product> >((ct) =>
            {
                var result = products.AsParallel();
                try
                {
                    Console.WriteLine("开始执行 tk1 任务", products.Count);
                    Console.WriteLine("tk1 任务中 数据结果集数量为:{0}", products.Count);

                    result = products.AsParallel().WithCancellation(token).Where(p => p.Name.Contains("1") && p.Name.Contains("2"));


                    /*
                     * 指定查询时所需的并行度 WithDegreeOfParallelism
                     *  默认情况下,PLINQ总是会试图利用所有的可用逻辑内核达到最佳性能,
                     *  在程序中我们可以利用WithDegreeOfParallelism方法指定一个不同最大并行度。
                     *
                     * 好处:如果计算机有8个可用的逻辑内核,PLINQ查询最多运行4个并发任务,
                     * 这样可用使用Parallel.Invoke 加载多个带有不同并行度的PLINQ查询,
                     * 有一些PLINQ查询的可扩展性有限,因此这些选项可用让您充分利用额外的内核。
                     *
                     *
                     * tk1任务 采用所有可用处理器*/
                    // result = products.AsParallel().WithCancellation(token).WithDegreeOfParallelism(Environment.ProcessorCount).Where(p => p.Name.Contains("1") && p.Name.Contains("2") && p.Category.Contains("1") && p.Category.Contains("2"));
                    /*tk1任务 采用1个可用处理器*/
                    // result = products.AsParallel().WithCancellation(token).WithDegreeOfParallelism(1).Where(p => p.Name.Contains("1") && p.Name.Contains("2") && p.Category.Contains("1") && p.Category.Contains("2"));
                }
                catch (AggregateException ex)
                {
                    foreach (Exception e in ex.InnerExceptions)
                    {
                        Console.WriteLine("tk3 错误:{0}", e.Message);
                    }
                }
                return(result);
            }, cts.Token);

            /*创建tk2 任务,在执行tk1任务完成  基于tk1的结果查询 符合 条件的数据*/
            Task <ParallelQuery <Product> > tk2 = tk1.ContinueWith <ParallelQuery <Product> >((tk) =>
            {
                var result = tk.Result;
                try
                {
                    Console.WriteLine("开始执行 tk2 任务", products.Count);
                    Console.WriteLine("tk2 任务中 数据结果集数量为:{0}", tk.Result.Count());
                    result = tk.Result.WithCancellation(token).Where(p => p.Category.Contains("1") && p.Category.Contains("2"));
                }
                catch (AggregateException ex)
                {
                    foreach (Exception e in ex.InnerExceptions)
                    {
                        Console.WriteLine("tk3 错误:{0}", e.Message);
                    }
                }
                return(result);
            }, TaskContinuationOptions.OnlyOnRanToCompletion);

            /*创建tk3 任务,在执行tk1任务完成  基于tk1的结果查询 符合 条件的数据*/
            Task <ParallelQuery <Product> > tk3 = tk1.ContinueWith <ParallelQuery <Product> >((tk) =>
            {
                var result = tk.Result;
                try
                {
                    Console.WriteLine("开始执行 tk3 任务", products.Count);
                    Console.WriteLine("tk3 任务中 数据结果集数量为:{0}", tk.Result.Count());
                    result = tk.Result.WithCancellation(token).Where(p => p.SellPrice > 1111 && p.SellPrice < 222222);
                }
                catch (AggregateException ex)
                {
                    foreach (Exception e in ex.InnerExceptions)
                    {
                        Console.WriteLine("tk3 错误:{0}", e.Message);
                    }
                }
                return(result);
            }, TaskContinuationOptions.OnlyOnRanToCompletion);

            tk1.Start();

            try
            {
                Thread.Sleep(10);
                cts.Cancel();//取消任务
                Task.WaitAll(tk1, tk2, tk3);

                Console.WriteLine("tk2任务结果输出,筛选后记录总数为:{0}", tk2.Result.Count());
                Console.WriteLine("tk3任务结果输出,筛选后记录总数为:{0}", tk3.Result.Count());
            }
            catch (AggregateException ex)
            {
                foreach (Exception e in ex.InnerExceptions)
                {
                    Console.WriteLine("错误:{0}", e.Message);
                }
            }

            tk1.Dispose();
            tk2.Dispose();
            tk3.Dispose();
            cts.Dispose();
            Console.ReadLine();
        }
コード例 #16
0
        public static void TestMethod1()
        {
            var products = new ConcurrentQueue <string>();

            products.Enqueue("E");
            products.Enqueue("F");
            products.Enqueue("B");
            products.Enqueue("G");
            products.Enqueue("A");
            products.Enqueue("C");
            products.Enqueue("SS");
            products.Enqueue("D");

            /*不采用并行化  其数据输出结果  不做任何处理   */
            var productListLinq = from product in products
                                  where (product.Length == 1)
                                  select product;

            var appendStr = string.Empty;

            foreach (var str in productListLinq)
            {
                appendStr += str + " ";
            }
            Console.WriteLine($"不采用并行化 输出:{appendStr}");

            /*不采用任何排序策略  其数据输出结果 是直接将分区数据结果合并起来 不做任何处理   */
            var productListPLinq = from product in products.AsParallel()
                                   where (product.Length == 1)
                                   select product;

            appendStr = string.Empty;
            foreach (var str in productListPLinq)
            {
                appendStr += str + " ";
            }
            Console.WriteLine($"不采用AsOrdered 输出:{appendStr}");

            /*采用 AsOrdered 排序策略  其数据输出结果 是直接将分区数据结果合并起来 并按原始数据顺序排序*/
            var productListPLinq1 = from product in products.AsParallel().AsOrdered()
                                    where (product.Length == 1)
                                    select product;

            appendStr = string.Empty;
            foreach (var str in productListPLinq1)
            {
                appendStr += str + " ";
            }
            Console.WriteLine($"采用AsOrdered 输出:{appendStr}");

            /*采用 orderby 排序策略  其数据输出结果 是直接将分区数据结果合并起来 并按orderby要求进行排序*/
            var productListPLinq2 = from product in products.AsParallel()
                                    where (product.Length == 1)
                                    orderby product
                                    select product;

            appendStr = string.Empty;
            foreach (var str in productListPLinq2)
            {
                appendStr += str + " ";
            }
            Console.WriteLine($"采用orderby 输出:{appendStr}");

            Console.ReadLine();
        }
コード例 #17
0
ファイル: Program.cs プロジェクト: ranjan545486/client-sample
        public async static Task Main(string[] args)
        {
            //by default .net will allow only 2 connections at one time.
            ServicePointManager.DefaultConnectionLimit = 10000;
            //var query =
            // from b in Enumerable.Range(1, 20).Batch(3)
            // select string.Join(", ", b);
            //Array.ForEach(query.ToArray(), Console.WriteLine);

            List <int> lst = new List <int>();

            //for (int i = 0; i < 200; i++){
            //   if(i%2 ==0)
            //    lst.Add(i);
            //}

            Console.WriteLine(DateTime.Now);
            ConcurrentQueue <int> queue = new ConcurrentQueue <int>();

            for (int i = 0; i < 10000; i++)
            {
                queue.Enqueue(i);
            }

            var queueQuery = queue.AsParallel().Partition(25);


            //var taskList = new List<Task<Uri>>();


            // works fine
            //foreach(var b in queueQuery){

            //    Data d = new Data();
            //    var client = new HttpClient();
            //    d.productIds = b.ToList();
            //   // taskList.Add(GetResponseAsync(d));
            //    await CreateProductAsync(client, d);
            //    b.ToList().ForEach(o=> Console.WriteLine(o));
            //}

            //works partially
            try{
                await Task.WhenAll(queueQuery.Select(i => FireQuery(i)));
            }catch (Exception)
            {
                _httpClient.CancelPendingRequests();
            }

            foreach (var b in queueQuery)
            {
                b.ToList().ForEach(o => Console.WriteLine(o));
            }


            Console.WriteLine("the total queue items processed in this batch is ");


            // works with errors
            //foreach(var b in queueQuery){
            //    Data d = new Data();
            //    HttpClient client = new HttpClient();
            //    d.productIds = b.ToList();
            //    taskList.Add(GetResponseAsync(d));
            //}

            //try
            //{
            //    await Task.WhenAll(taskList.ToArray());
            //}
            //catch (Exception)
            //{

            //    return;
            //}

            Console.WriteLine(counter);

            Console.WriteLine(DateTime.Now);
            queue.Clear();
        }
コード例 #18
0
        private static void _processQueueBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            while (queueing || FileInfoQueue.Count() > 0)
            {
                //FileInfo file = null;
                //FileInfoQueue.TryDequeue(out file);

                //if (file != null)
                //{
                //    string _genuineFolderFullName = System.IO.Path.GetDirectoryName(file.FullName);

                //    if (!_genuineFolderFullName.Equals(GlobalSettings.systemdrive.Name))
                //    {
                //        string _folderFullName = _genuineFolderFullName;

                //        if (_genuineFolderFullName.Split('\\').Count() > maxFolderDepth)
                //        {
                //            _folderFullName = _genuineFolderFullName.Substring(0, FindNthOccur(_genuineFolderFullName, '\\', maxFolderDepth));
                //        }

                //        lock (_locker)
                //        {
                //            long _fn;
                //            if (FolderInformationList.TryGetValue(_folderFullName, out _fn))
                //            {
                //                FolderInformationList[_folderFullName] = _fn + file.Length;
                //            }
                //            else
                //            {
                //                FolderInformationList.TryAdd(_folderFullName, file.Length);
                //            }
                //        }
                //    }
                //}

                SemaphoreSlim semaphoreSlim = new SemaphoreSlim(4, 4);
                FileInfoQueue.AsParallel().AsOrdered().ForAll(item =>
                {
                    try
                    {
                        semaphoreSlim.Wait();

                        FileInfo file = null;
                        FileInfoQueue.TryDequeue(out file);

                        if (file != null)
                        {
                            string _genuineFolderFullName = System.IO.Path.GetDirectoryName(file.FullName);

                            if (!_genuineFolderFullName.Equals(GlobalSettings.systemdrive.Name) && DirectoryIsNotExcluded(file.FullName))
                            {
                                string _folderFullName = _genuineFolderFullName;

                                if (_genuineFolderFullName.Split('\\').Count() > maxFolderDepth)
                                {
                                    _folderFullName = _genuineFolderFullName.Substring(0, FindNthOccur(_genuineFolderFullName, '\\', maxFolderDepth));
                                }

                                lock (_locker)
                                {
                                    long _fn;
                                    if (FolderInformationList.TryGetValue(_folderFullName, out _fn))
                                    {
                                        FolderInformationList[_folderFullName] = _fn + file.Length;
                                    }
                                    else
                                    {
                                        FolderInformationList.TryAdd(_folderFullName, file.Length);
                                    }
                                }
                            }
                        }
                    }
                    finally
                    {
                        semaphoreSlim.Release();
                    }
                });
#if DEBUG
                //Trace.WriteLine($"FileInfoQueue: {FileInfoQueue.Count()} | FolderInformationList: {FolderInformationList.Count}");
                //Trace.WriteLine($">>>>>>>>>>>>>>>>>>>>>> Queueing: {queueing}");
#endif
            }
#if DEBUG
            Console.WriteLine($"FileInfoQueue: {FileInfoQueue.Count()}");
#endif
            queueprocessed = true;
        }
コード例 #19
0
        public static void TestMethod1()
        {
            Console.WriteLine("当前计算机处理器数:{0}", Environment.ProcessorCount);
            ConcurrentQueue <Product> products = new ConcurrentQueue <Product>();

            /*向集合中添加多条数据*/
            Parallel.For(0, 600000, (num) =>
            {
                products.Enqueue(new Product()
                {
                    Category = "Category" + num, Name = "Name" + num, SellPrice = num
                });
            });

            Stopwatch sw = new Stopwatch();

            Thread.Sleep(1000);
            sw.Restart();
            var  count = 0;
            Task tk1   = Task.Factory.StartNew(() =>
            {
                var result = products.AsParallel().WithMergeOptions(ParallelMergeOptions.AutoBuffered).Where(p => p.Name.Contains("1") && p.Name.Contains("2") && p.Category.Contains("1") && p.Category.Contains("2"));
                count      = result.Count();
            });

            Task.WaitAll(tk1);
            sw.Stop();
            Console.WriteLine("ParallelMergeOptions.AutoBuffered 耗时:{0},数量:{1}", sw.ElapsedMilliseconds, count);

            sw.Restart();
            var  count1 = 0;
            Task tk2    = Task.Factory.StartNew(() =>
            {
                var result = products.AsParallel().WithMergeOptions(ParallelMergeOptions.Default).Where(p => p.Name.Contains("1") && p.Name.Contains("2") && p.Category.Contains("1") && p.Category.Contains("2"));
                count1     = result.Count();
            });

            Task.WaitAll(tk2);
            sw.Stop();
            Console.WriteLine("ParallelMergeOptions.Default 耗时:{0},数量:{1}", sw.ElapsedMilliseconds, count1);


            sw.Restart();
            var  count2 = 0;
            Task tk3    = Task.Factory.StartNew(() =>
            {
                var result = products.AsParallel().WithMergeOptions(ParallelMergeOptions.FullyBuffered).Where(p => p.Name.Contains("1") && p.Name.Contains("2") && p.Category.Contains("1") && p.Category.Contains("2"));
                count2     = result.Count();
            });

            Task.WaitAll(tk3);
            sw.Stop();
            Console.WriteLine("ParallelMergeOptions.FullyBuffered 耗时:{0},数量:{1}", sw.ElapsedMilliseconds, count2);


            sw.Restart();
            var  count3 = 0;
            Task tk4    = Task.Factory.StartNew(() =>
            {
                var result = products.AsParallel().WithMergeOptions(ParallelMergeOptions.NotBuffered).Where(p => p.Name.Contains("1") && p.Name.Contains("2") && p.Category.Contains("1") && p.Category.Contains("2"));
                count3     = result.Count();
            });

            Task.WaitAll(tk4);
            sw.Stop();
            Console.WriteLine("ParallelMergeOptions.NotBuffered 耗时:{0},数量:{1}", sw.ElapsedMilliseconds, count3);

            tk4.Dispose();
            tk3.Dispose();
            tk2.Dispose();
            tk1.Dispose();
            Console.ReadLine();
        }