Question

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. 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?

Était-ce utile?

La solution

I think the simplest way is to use a BufferBlock combined with a Task that actually produces the items. 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;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top