本文整理汇总了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();
}
}
示例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);
}
}