Question

I wonder whether it is possible to subscribe to a one-time notification when the first item is received in the input buffer or processed in a data block. I am aware I can set a flag within the data block but that would create overhead as it would run check the flag on each new item. My data block processes several million items thus this flag adds unnecessary overhead.

Is there a better way to be notified on the first incoming item?

Was it helpful?

Solution

You could try creating a link with MaxMessages=1 to a block that processes the notification. This link would be removed after delivering a single message.

OTHER TIPS

(I know this is 11 months too late ... but I'm answering in the hope that someone will tell me why this isn't a good idea.)

You have a particular block X where you want to know when it gets its first message. It has an upstream block. Insert between the upstream block and block X a TransformBlock, linking it from the upstream block with MaxMessages=1 so that it gets immediately unlinked when it gets the first message. That also blocks the upstream block. The inserted TransformBlock's Func does 3 things in its delegate:

  1. Does whatever notification you'd like that the "first block was received".
  2. Links its successor block X to the upstream block.
  3. Returns its argument unchanged to get passed to block X.

In other words ... the inserted block accepts and passes on the first data item and is spliced out of the dataflow.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top