简体   繁体   English

tpl数据流:生产者启动块

[英]tpl dataflow: producer startblock

Im trying to build up a pipeline, where the first block will be the producer block. 我试图建立一个管道,第一个块将是生产者块。 It gets in an Enum object and according to this it produces a lot of data. 它进入一个Enum对象,并据此产生大量数据。 This data should be sent to the following steps of the pipeline automatically as it arrives. 这些数据到达时应自动发送到管道的以下步骤。

Is there any way to do this? 有什么办法吗? Or do I need to create a customblock? 还是我需要创建一个customblock?

I think the simplest way is to use a BufferBlock combined with a Task that actually produces the items. 我认为最简单的方法是将BufferBlock与实际产生项目的Task结合使用。 Something like: 就像是:

public ISourceBlock<Foo> CreateProducer()
{
    var block = new BufferBlock<Foo>(); // add options if necessary

    Task.Run(() =>
    {
        try
        {
            while (whatever)
            {
                Foo foo = …;
                block.Post(foo); // or await SendAsync() if block is bounded
            }
        }
        catch (Exception ex)
        {
            ((IDataflowBlock)block).Fault(ex);
        }
    });

    return block;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM