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


C# BlockingCollection.Count方法代码示例

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


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

示例1: RunLoadBalancedPipeline

        /// <summary>
        /// Run a variation of the pipeline that uses a user-specified number of tasks for the filter stage.
        /// </summary>
        /// <param name="fileNames">List of image file names in source directory</param>
        /// <param name="sourceDir">Name of directory of source images</param>
        /// <param name="queueLength">Length of image queue</param>
        /// <param name="displayFn">Display action</param>
        /// <param name="cts">Cancellation token</param>
        /// <param name="filterTaskCount">Number of filter tasks</param>
        static void RunLoadBalancedPipeline(IEnumerable<string> fileNames, string sourceDir, int queueLength, Action<ImageInfo> displayFn,
            CancellationTokenSource cts, int filterTaskCount)
        {
            // Create data pipes 
            var originalImages = new BlockingCollection<ImageInfo>(queueLength);
            var thumbnailImages = new BlockingCollection<ImageInfo>(queueLength);
            var filteredImageMultiplexer = new BlockingMultiplexer<ImageInfo>(info => info.SequenceNumber, 0, queueLength);
            var filteredImagesCollections = (BlockingCollection<ImageInfo>[])Array.CreateInstance(
                                   typeof(BlockingCollection<ImageInfo>), filterTaskCount);

            try
            {
                // Start pipelined tasks
                Action<ImageInfo> updateStatisticsFn = info =>
                {
                    info.QueueCount1 = originalImages.Count();
                    info.QueueCount2 = thumbnailImages.Count();
                    info.QueueCount3 = filteredImageMultiplexer.Count;
                };
                const TaskCreationOptions options = TaskCreationOptions.LongRunning;
                var f = new TaskFactory(CancellationToken.None, options, TaskContinuationOptions.None, TaskScheduler.Default);
                Task[] tasks = (Task[])Array.CreateInstance(typeof(Task), filterTaskCount + 3);
                int taskId = 0;

                tasks[taskId++] = f.StartNew(() =>
                      LoadPipelinedImages(fileNames, sourceDir, originalImages, cts));

                tasks[taskId++] = f.StartNew(() =>
                      ScalePipelinedImages(originalImages, thumbnailImages, cts));

                for (int i = 0; i < filterTaskCount; i++)
                {
                    var tmp = i;
                    filteredImagesCollections[tmp] = filteredImageMultiplexer.GetProducerQueue();
                    tasks[taskId++] = f.StartNew(() => FilterPipelinedImages(thumbnailImages, filteredImagesCollections[tmp], cts));
                }

                tasks[taskId++] = f.StartNew(() =>
                      DisplayPipelinedImages(filteredImageMultiplexer.GetConsumingEnumerable(), displayFn, 
                                             updateStatisticsFn, cts));

                Task.WaitAll(tasks);
            }
            finally
            {
                // there might be cleanup in the case of cancellation or an exception.
                DisposeImagesInQueue(originalImages);
                DisposeImagesInQueue(thumbnailImages);
                foreach (var filteredImages in filteredImagesCollections)
                    DisposeImagesInQueue(filteredImages);
                foreach (var info in filteredImageMultiplexer.GetCleanupEnumerable())
                    info.Dispose();
            }
        }
开发者ID:arthis,项目名称:parallelPatterns,代码行数:63,代码来源:ImagePipeline.cs

示例2: RunPipelined

        /// <summary>
        /// Run the image processing pipeline.
        /// </summary>
        /// <param name="fileNames">List of image file names in source directory</param>
        /// <param name="sourceDir">Name of directory of source images</param>
        /// <param name="queueLength">Length of image queue</param>
        /// <param name="displayFn">Display action</param>
        /// <param name="cts">Cancellation token</param>
        static void RunPipelined(IEnumerable<string> fileNames, string sourceDir, int queueLength, Action<ImageInfo> displayFn,
            CancellationTokenSource cts)
        {
            // Data pipes 
            var originalImages = new BlockingCollection<ImageInfo>(queueLength);
            var thumbnailImages = new BlockingCollection<ImageInfo>(queueLength);
            var filteredImages = new BlockingCollection<ImageInfo>(queueLength);
            try
            {
                var f = new TaskFactory(TaskCreationOptions.LongRunning, TaskContinuationOptions.None);
                Action<ImageInfo> updateStatisticsFn = info =>
                {
                    info.QueueCount1 = originalImages.Count();
                    info.QueueCount2 = thumbnailImages.Count();
                    info.QueueCount3 = filteredImages.Count();
                };

                // Start pipelined tasks
                var loadTask = f.StartNew(() =>
                      LoadPipelinedImages(fileNames, sourceDir, originalImages, cts));

                var scaleTask = f.StartNew(() =>
                      ScalePipelinedImages(originalImages, thumbnailImages, cts));

                var filterTask = f.StartNew(() =>
                      FilterPipelinedImages(thumbnailImages, filteredImages, cts));

                var displayTask = f.StartNew(() =>
                      DisplayPipelinedImages(filteredImages.GetConsumingEnumerable(), 
                           displayFn, updateStatisticsFn, cts));

                Task.WaitAll(loadTask, scaleTask, filterTask, displayTask);
            }
            finally
            {
                // in case of exception or cancellation, there might be bitmaps
                // that need to be disposed.
                DisposeImagesInQueue(originalImages);
                DisposeImagesInQueue(thumbnailImages);
                DisposeImagesInQueue(filteredImages);                
            }
        }
开发者ID:arthis,项目名称:parallelPatterns,代码行数:50,代码来源:ImagePipeline.cs


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