当前位置: 首页>>代码示例>>C#>>正文


C# BlockingCollection.ToList方法代码示例

本文整理汇总了C#中BlockingCollection.ToList方法的典型用法代码示例。如果您正苦于以下问题:C# BlockingCollection.ToList方法的具体用法?C# BlockingCollection.ToList怎么用?C# BlockingCollection.ToList使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BlockingCollection的用法示例。


在下文中一共展示了BlockingCollection.ToList方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: DetachedParentChild

        public void DetachedParentChild()
        {
            //Arrange
              var sequence = new BlockingCollection<int>();
              var child1 = new Action(() => { sequence.Add(2); Thread.Sleep(5000); sequence.Add(4); });
              var parent = new Action(() => { sequence.Add(1); Task.Factory.StartNew(child1); Thread.Sleep(2500); sequence.Add(3); });

              //Act
              var parent_task = Task.Factory.StartNew(parent);
              parent_task.Wait();

              //Assert
              Assert.IsTrue(Enumerable.SequenceEqual(new int[] { 1, 2, 3 }, sequence), sequence.Aggregate(new StringBuilder(), (whole, next) => whole.AppendFormat("{0}|", next)).ToString());
              CollectionAssert.AreEqual(new int[] { 1, 2, 3 }, sequence.ToList());
        }
开发者ID:MarcoDorantes,项目名称:spike,代码行数:15,代码来源:TaskSyncsSpec.cs

示例2: LoadBuildersInParallel

        public static List<Builder> LoadBuildersInParallel(int numberOfBuilders)
        {
            BlockingCollection<Builder> buildersToLoad = new BlockingCollection<Builder>();
            BlockingCollection<Builder> loadedBuilders = new BlockingCollection<Builder>();

            for (int i = 0; i < numberOfBuilders; i++)
            {
                buildersToLoad.Add(new Builder { Name = "Builder" + i, Status = "Status" + i });
            }

            Parallel.ForEach(buildersToLoad, new ParallelOptions { MaxDegreeOfParallelism = 100 }, currentBuilder =>
            {
              //database load/instansiation of objects
               Thread.Sleep(1000);
                loadedBuilders.Add(currentBuilder);
            });

            return loadedBuilders.ToList();
        }
开发者ID:cmgross,项目名称:WebFormsTPL,代码行数:19,代码来源:Builder.cs

示例3: Execute

        public List<int> Execute(int minPrime, int maxPrime, int degree)
        {
            _primes = new BlockingCollection<int> {2, 3, 5, 7};
            _candidates = new BlockingCollection<int>();
            var subTasks = new Task[degree];

            for (int i = 0; i < degree; i++)
            {
                subTasks[i] = new Task(() => SubTask(_candidates, _primes));
                subTasks[i].Start();
            }

            for (int c = 8; c < maxPrime; c++) _candidates.Add(c);
            _candidates.CompleteAdding();

            Task.WaitAll(subTasks);

            return _primes.ToList();
        }
开发者ID:hypertheory-training,项目名称:PrimesExamples,代码行数:19,代码来源:PipelinedPrimes.cs

示例4: GetFilesContainesStringParallel

 private static List<FileInfo> GetFilesContainesStringParallel(List<FileInfo> filesList, string str)
 {
     Console.ForegroundColor = ConsoleColor.Magenta;
     Stopwatch leadTime = Stopwatch.StartNew();
     BlockingCollection<FileInfo> filesThatContainedString = new BlockingCollection<FileInfo>();
     ParallelLoopResult loopResult = Parallel.ForEach(filesList, (file) =>
     {
         if (FindString(file, str)) filesThatContainedString.Add(file);
     });
     if (!loopResult.IsCompleted)
     {
         Console.WriteLine("Fail on iteration " + loopResult.LowestBreakIteration + "\n");
     }
     leadTime.Stop();
     Console.WriteLine("GetFilesContainesStringParallel() leadTime: {0} ms", leadTime.ElapsedMilliseconds);
     Console.ForegroundColor = ConsoleColor.White;
     return filesThatContainedString.ToList();
 }
开发者ID:oliver91,项目名称:HW_TPL,代码行数:18,代码来源:Program.cs

示例5: LoadBuildersWithTasks

        public static List<Builder> LoadBuildersWithTasks(int numberOfBuilders)
        {
            BlockingCollection<Builder> buildersToLoad = new BlockingCollection<Builder>();
            BlockingCollection<Builder> loadedBuilders = new BlockingCollection<Builder>();

            for (int i = 0; i < numberOfBuilders; i++)
            {
                buildersToLoad.Add(new Builder { Name = "Builder" + i, Status = "Status" + i });
            }
            buildersToLoad.CompleteAdding();

            Task loader1 = Task.Factory.StartNew(() =>
                {
                    foreach (Builder item in buildersToLoad.GetConsumingEnumerable())
                    {
                        Thread.Sleep(1000);
                        loadedBuilders.Add(item);
                    }
                }, TaskCreationOptions.LongRunning);

            Task loader2 = Task.Factory.StartNew(() =>
            {
                foreach (Builder item in buildersToLoad.GetConsumingEnumerable())
                {
                    Thread.Sleep(1000);
                    loadedBuilders.Add(item);
                }
            }, TaskCreationOptions.LongRunning);

            Task.WaitAll(loader1, loader2);
            return loadedBuilders.ToList();
        }
开发者ID:cmgross,项目名称:WebFormsTPL,代码行数:32,代码来源:Builder.cs

示例6: GetPartitionsWithPendingEvents

         //<summary>
         //Gets the list of all partitions that have pending unpublished events.
         //</summary>
         //<returns>The list of all partitions.</returns>
        public IEnumerable<string> GetPartitionsWithPendingEvents()
        {
            var eventTableServiceEntities = new TableQuery<EventTableServiceEntity>();
            var table = this.tableClient.GetTableReference(this.tableName);

            var query = new TableQuery<EventTableServiceEntity>()
                //.Where(
                //TableQuery.CombineFilters(
                //    TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.GreaterThanOrEqual, UnpublishedRowKeyPrefix),
                //    TableOperators.And,
                //    TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.LessThanOrEqual, UnpublishedRowKeyPrefixUpperLimit)))
                   // .Select(x => new { x.PartitionKey })
                    .AsTableQuery();

            //var query = eventTableServiceEntities
            //    .Where(
            //        x =>
            //        String.Compare(x.RowKey, UnpublishedRowKeyPrefix, StringComparison.Ordinal) >= 0 &&
            //        String.Compare(x.RowKey, UnpublishedRowKeyPrefixUpperLimit, StringComparison.Ordinal) <= 0)
            //    .Select(x => new { x.PartitionKey });


            var result = new BlockingCollection<string>();
            
            var continuationToken = new TableContinuationToken();
            var queryResult = table.ExecuteQuerySegmentedAsync(query, continuationToken).Result;
            foreach (var key in queryResult.Results.Select(x => x.PartitionKey).Distinct())
            {
                result.Add(key);
            }

            return result.ToList();
        }
开发者ID:wayne-o,项目名称:delete-me,代码行数:37,代码来源:EventStore.cs


注:本文中的BlockingCollection.ToList方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。