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


C# BufferBlock.PropagateCompleted方法代码示例

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


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

示例1: GetFileWordSaverBlock

        public static IPropagatorBlock<FileWord, FileWord> GetFileWordSaverBlock(Func<IRepository> repositoryFactory)
        {
            var inputBuffer = new BufferBlock<FileWord>();
            var outputBuffer = new BufferBlock<FileWord>();
            
            var batchBlockWithoutParents = new BatchBlock<FileWord>(1000);
            inputBuffer.LinkTo(batchBlockWithoutParents, i => i.File.FileId > 0 && i.Word.WordId > 0);
            inputBuffer.PropagateCompleted(batchBlockWithoutParents);
            var saveWithoutParentsBlock = new TransformManyBlock<FileWord[], FileWord>(
                async fileWords =>
                {
                    return await SaveEntities(repositoryFactory, fileWords, true);
                },
                new ExecutionDataflowBlockOptions
                {
                    MaxDegreeOfParallelism = Utils.GlobalMaxDegreeOfParallelism
                });
            batchBlockWithoutParents.LinkToAndPropagateCompleted(saveWithoutParentsBlock);
            saveWithoutParentsBlock.LinkTo(outputBuffer);

            var batchBlockWithParents = new BatchBlock<FileWord>(300);

            // MaxMessages value means inputBuffer will unlink automatically from batchBlockWithParents after 300 messages.
            // This is required to prohibit "stealing" of workload from saveWithoutParentsBlock
            inputBuffer.LinkTo(batchBlockWithParents, new DataflowLinkOptions { MaxMessages = 300 });
            inputBuffer.PropagateCompleted(batchBlockWithParents);

            var saveWithParentsBlock = new TransformManyBlock<FileWord[], FileWord>(
                async fileWords =>
                {
                    var res = await SaveEntities(repositoryFactory, fileWords, false);
                    FileWord fileWord;
                    if (inputBuffer.TryReceive(out fileWord)) // This unblocks inputBuffer due to unlinking from batchBlockWithParents
                    {
                        if (fileWord.File.FileId > 0 && fileWord.Word.WordId > 0)
                        {
                            batchBlockWithoutParents.Post(fileWord);
                        }
                        else
                        {
                            batchBlockWithParents.Post(fileWord);
                        }
                    }

                    // Link again for another 300 messages
                    inputBuffer.LinkTo(batchBlockWithParents, new DataflowLinkOptions { MaxMessages = 300 });
                    return res;
                },
                new ExecutionDataflowBlockOptions
                {
                    MaxDegreeOfParallelism = 1 // If we are saving files/words also we cannot do this concurrently or duplicates of files/words may be inserted
                });
            batchBlockWithParents.LinkToAndPropagateCompleted(saveWithParentsBlock);
            saveWithParentsBlock.LinkTo(outputBuffer);

            saveWithoutParentsBlock.Completion.ContinueWith(
                t => ((IDataflowBlock)outputBuffer).Fault(t.Exception),
                TaskContinuationOptions.OnlyOnFaulted);
            saveWithParentsBlock.Completion.ContinueWith(
                t => ((IDataflowBlock)outputBuffer).Fault(t.Exception),
                TaskContinuationOptions.OnlyOnFaulted);
            Task.WhenAll(saveWithoutParentsBlock.Completion, saveWithParentsBlock.Completion)
                .ContinueWith(t => outputBuffer.Complete(), TaskContinuationOptions.NotOnFaulted);

            return DataflowBlock.Encapsulate(inputBuffer, outputBuffer);
        }
开发者ID:eugene-sea,项目名称:FileWordsDataflow,代码行数:66,代码来源:FileWordSaver.cs


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